1

I am new xml parsing in android. The below is the xml string I have. I am getting this XML string data from onActivityResult method.So the below data saved in one string variable like resultText; I want to read each attribute values from below string.

<?xml version="1.0" encoding="UTF-8"?>
<PrintLetterBarcodeData uid="521007171049" 
name="Bandigari Katamaraju"
 gender="M"
 yob="1991" 
 co="S/O: BANDIGARI YADAGIRI" 
 house="4-141/1"
 loc="EDULLAGUDEM" 
 vtc="Edullagudam"
 dist="Nalgonda"
 subdist="For New VTC"
 state="Andhra Pradesh" 
 pc="508112"/>

I want to read the each attribute of that single node. Thanks in advance.

katamaraju
  • 33
  • 6
  • 1
    checkout this answer: http://stackoverflow.com/a/7607445/2674225 You can parse it to `Node` and get attributes (http://developer.android.com/reference/org/w3c/dom/Node.html#getAttributes()). Or you can simply split it and read attributes manually, but it's less elegant ;) – rzysia Sep 30 '15 at 13:37
  • Thanks for reply, but I didn't get you. I want to read each attribute value. The code you referred is not exactly belongs to the same. How can I achieve this by split? – katamaraju Sep 30 '15 at 13:45
  • When you parse this xm to Node, you will be able to get all this node attibutes via method `getAttributes()`, which will return them as `NamedNodeMap` object. You have to know, that everythinh is a node - even attibutes are nodes. And you have all of them it this returned object. Then you can simple iterate over it ex. in `for` loop and you can access attribute by `obj.item(i)` and call, ex. `getNodeName()` or `getNodeValue()`. – rzysia Sep 30 '15 at 13:52

1 Answers1

0

Use an XMLParser (check this page), one that works nicely is the XmlPullParser.

You can initialize your parser with:

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();

xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );

Than you can iterate over the complete XML object using

int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
    /*YOUR CODE HERE*/
    eventType = xpp.next();
}

Where you can check for the eventTypes : START_DOCUMENT, START_TAG, END_TAG, and TEXT.
Once you are in either start or end tag you can get the name of the tag using getName(), at the TEXT event type you can use getText() and you can use, at the BEGIN_TAG, the functions getAttributeCount(), getAttributeName(index) and getAttributeValue() you can get all the attributes belonging to each tag.


In your specific case

You can use something like this:

String xmlString = YOURSTRING_HERE;
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput( new StringReader ( xmlString ) );
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
    if(eventType == XmlPullParser.START_DOCUMENT) {
        System.out.println("Start document");
    } else if(eventType == XmlPullParser.START_TAG) {
        System.out.println("Start tag "+xpp.getName());
        if (xpp.getName().equals("PrintLetterBarcodeData")){
            for (int i=0; i<xpp.getAttributeCount(); i++){
                 System.out.println("attribute:"+xpp.getAttributeName(i)+" with value: "+xpp.getAttributeValue(i))
                 //Store here your values in the variables of your choice.
            }
        }
    } else if(eventType == XmlPullParser.END_TAG) {
        System.out.println("End tag "+xpp.getName());
    } else if(eventType == XmlPullParser.TEXT) {
        System.out.println("Text "+xpp.getText());
    }
    eventType = xpp.next();
}
System.out.println("End document");
Niki van Stein
  • 10,564
  • 3
  • 29
  • 62