簡體   English   中英

如何在Android中解析這種XML

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

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

看來您不知道如何解析節點的屬性。

使用DOM解析器,可以使用Node的getAttributes()方法來訪問屬性,而使用SAX解析器,則可以使用XmlPullParser類的getAttributeValue()

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

檢查此鏈接,它將提供自動類,該類將解析您的所有xml數據。 該站點具有生成器,該生成器將生成可在項目中使用的Java類。

檢查此鏈接

我通過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