如何在android中解析这种XML

时间:2015-06-02 08:23:00

标签: java android saxparser xmlpullparser

我可以使用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>

4 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

解析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.

答案 2 :(得分:0)

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

Check this link

答案 3 :(得分:0)

我通过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);
        }
}