【问题标题】:How to parse this kind of XML in android如何在android中解析这种XML
【发布时间】:2015-06-02 08:23:00
【问题描述】:

我可以使用 SAX、XMLPullParser,我可以解析通用格式的数据。但我正在努力解析这种格式化的 XML 数据,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Data Branch="True" >
    <Branch
        BranchClosingDate=""
        BranchOpeningDate="01/01/1990 00:00:00"
        DistrictId="19"
        Id="981"
        IsActive="True"
        IsLocal="True"
        LocalName="154"
        LocationType="1"
        MobileNumber="123"
        Name="Dhaperhat" />
</Data>

【问题讨论】:

  • 您面临什么问题? Android有xml拉解析器,你可以轻松解析xml。
  • 我可以解析这种 XML 数据:112010109Y2.007.99 但是上面的格式让我很烦……
  • 您的 XML 很简单,您有 Branch 标签,并且该标签有多个属性。所以检查谷歌如何使用拉解析器获取标签的属性值。
  • 我仍然不清楚您的实际问题是什么。能详细点吗?
  • 是的,我想告诉你的是一样的:) ...

标签: java android saxparser xmlpullparser


【解决方案1】:

您似乎不知道如何解析节点的属性。

使用 DOM 解析器,您可以使用 Node 的 getAttributes() 方法来访问属性,使用 SAX 解析器,您可以使用 XmlPullParser 类的 getAttributeValue()

【讨论】:

  • public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { currentElement = true; if (qName.equals("Branch")) { int length = attributes.getLength(); for (int i = 0; i
  • 因此,无论如何,当您获得属性时,这会更加简单。这些天我没有做太多 XML,但似乎你只需要提示它是你所追求的属性。
【解决方案2】:

解析 XML 提要的步骤如下:

01 .As described in Analyze the Feed, identify the tags you want to include
   in your app. This example extracts data for the entry tag and its nested
   tags title, link, and summary.

02 .Create the following methods:
   -> A "read" method for each tag you're interested in. For example,
      readEntry(), readTitle(), and so on. The parser reads tags from 
      the input stream. When it encounters a tag named entry, title,
      link or summary, it calls the appropriate method for that tag. 
      Otherwise, it skips the tag.
   -> Methods to extract data for each different type of tag and to advance
      the parser to the next tag.For example:

         * For the title and summary tags, the parser calls readText(). 
           This method extracts data for these tags by calling   
           parser.getText().

         * For the link tag, the parser extracts data for links by first   
           determining if the link is the kind it's interested in. Then it 
           uses
           parser.getAttributeValue() to extract the link's value.

         * For the entry tag, the parser calls readEntry(). This method
           parses the entry's nested tags and returns an Entry object with
           the data members title, link, and summary.

    -> A helper skip() method that's recursive. For more discussion of this
       topic, see Skip Tags You Don't Care About.This snippet shows how the
       parser parses entries, titles, links, and summaries.

【讨论】:

    【解决方案3】:

    查看此链接,它将提供自动类来解析您的所有 xml 数据。该站点有生成器,可以生成可以在项目中使用的 java 类。

    Check this link

    【讨论】:

    • 他有解析xml而不是wsdl的问题。
    • 谢谢兄弟,我不是在搜索这种东西,我在 SD 卡中有一个本地 XML 文件,需要解析它。上面的格式让我很烦...
    【解决方案4】:

    我通过 SAX 和 DefaultHandler 解决了这个问题,

    public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
            currentElement = true;
            db = new DatabaseHelper(thecontext);
            if (qName.equals("Asa.Amms.Data.Entity.User")) {
                int length = attributes.getLength();
                for (int i = 0; i < length; i++) {
                    String name = attributes.getQName(i);
                    if (name.equals("Id")) {
                        id = Integer.parseInt(attributes.getValue(i));
                    }
                    if (name.equals("Login")) {
                        LoginID = attributes.getValue(i).toString();
                    }
                    if (name.equals("Name")) {
                        Name = attributes.getValue(i).toString();
                    }
                    if (name.equals("Password")) {
                        Password = attributes.getValue(i).toString();
                    }
                    if (name.equals("ProgramOfficerId")) {
                        user_ProgramOfficerId = Integer.parseInt(attributes.getValue(i).toString());
                    }
                }
                Log.i("Baal dhukbe", id + LoginID + Name + Password);
    
                db.insertUser(id, LoginID, Name, Password, user_ProgramOfficerId);
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      • 1970-01-01
      • 2021-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多