-1

I can use SAX, XMLPullParser, I can parse generalized format's data. But i am struggling to parse this formatted XML data like below:

<?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>
Miral Sarwar
  • 327
  • 6
  • 12

4 Answers4

1

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.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • 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 < length; i++) { String name = attributes.getQName(i); System.out.println("BranchClosingDate:" + name); String value = attributes.getValue(i); System.out.println("BranchOpeningDate:" + value); } } } I solved this in this way... – Miral Sarwar Jun 02 '15 at 09:12
  • So that's even simpler when you get handed the attributes anyways. I'm not doing much XML these days but it seemed that you just needed the hint that it's the attributes you are after. – Ridcully Jun 02 '15 at 10:42
1

The steps for parsing an XML feed are as follows:

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.
0

Check out this link, it will provide automated class which will parse your all xml data. The site has generator which will generate java classes which you can use in your project.

Check this link

Pankaj
  • 7,908
  • 6
  • 42
  • 65
0

I solved this through SAX and 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);
        }
}
Miral Sarwar
  • 327
  • 6
  • 12