Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I extract text from a nested xml using xmlpullparser in android?

<doc>
 <element>
  text
     <item>
     text1
     </item>
     <item>
     text2
     </item>
  text3
 </element>
 <element>
  another text
 </element>


 ...
</doc>

And I want to extract text to an string like this:

 "text text1 text2 text3"

"item" tags are nested inside "element"

Im using xmlpullparser in Android but I could use any other parser if it make things easier. tx.

like image 312
butelo Avatar asked Feb 27 '12 17:02

butelo


People also ask

What is XML pull parser in Android?

In android, the XMLPullParser interface provides the functionality to parse the XML files in android applications. The XMLPullParser is a simple and efficient parser method to parse the XML data when compared to other parser methods such as DOM Parser and SAX Parser.

What is pull parser?

XML Pull Parser is an interface that defines parsing functionality provided in XMLPULL V1 API (visit this website to learn more about API and its implementations).


1 Answers

I parse similar xml files. Here is an example. You'll need to add error checking as appropriate.

void loadXML(XmlPullParser xpp) {
    int eventType = xpp.next();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG && 0 == XML_ELEMENT_TAG.compareTo(xpp.getName())) {
            loadElement(xpp);
        }

        eventType = xpp.next();   
    }
}

private void loadElement(XmlPullParser xpp) {

    int eventType = xpp.getEventType();
    if ( eventType == XmlPullParser.START_TAG && 0 == XML_ELEMENT_TAG.compareTo(xpp.getName()) ) {
        eventType = xpp.next();
        while ( eventType != XmlPullParser.END_TAG || 0 != XML_ELEMENT_TAG.compareTo(xpp.getName()) ) {
            if (eventType == XmlPullParser.START_TAG &&     0 == XML_ITEM_TAG.compareTo(xpp.getName())) {
                loadItem(xpp);
            }

            eventType = xpp.next();   
        }
    } 
}

private void loadItem(XmlPullParser xpp) {

    int eventType = xpp.getEventType();
    if ( eventType == XmlPullParser.START_TAG && 0 == XML_ITEM_TAG.compareTo(xpp.getName()) ) {

        eventType = xpp.next();
        while ( eventType != XmlPullParser.END_TAG || 0 != XML_ITEM_TAG.compareTo(xpp.getName()) ) {

            // Get attributes.
            String  attr = xpp.getAttributeValue(null, XML_MY_ATTR); 
            String  text = null;

            // Get item text if present.
            eventType = xpp.next();
            while ( eventType != XmlPullParser.END_TAG || 0 != XML_ITEM_TAG.compareTo(xpp.getName()) ) {
                if ( eventType == XmlPullParser.TEXT ) {
                    text = xpp.getText();
                } 

                eventType = xpp.next();
            }

            eventType = xpp.next();   
        }
    } 
}
like image 118
jsmith Avatar answered Nov 15 '22 08:11

jsmith