简体   繁体   English

如何在Android中解析这种XML

[英]How to parse this kind of XML in android

I can use SAX, XMLPullParser, I can parse generalized format's data. 我可以使用SAX,XMLPullParser,也可以解析通用格式的数据。 But i am struggling to parse this formatted XML data like below: 但是我正在努力解析这种格式化的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>

It seems you don't know how to parse the attributes of the nodes. 看来您不知道如何解析节点的属性。

With DOM parser, you can use the getAttributes() method of a Node to access the attributes, with SAX parser you can use getAttributeValue() of the XmlPullParser class. 使用DOM解析器,可以使用Node的getAttributes()方法来访问属性,而使用SAX解析器,则可以使用XmlPullParser类的getAttributeValue()

The steps for parsing an XML feed are as follows: 解析XML feed的步骤如下:

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.

Check out this link, it will provide automated class which will parse your all xml data. 检查此链接,它将提供自动类,该类将解析您的所有xml数据。 The site has generator which will generate java classes which you can use in your project. 该站点具有生成器,该生成器将生成可在项目中使用的Java类。

Check this link 检查此链接

I solved this through SAX and DefaultHandler, 我通过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);
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM