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

5
  • If strings are all you're after, why not just you use the strings.xml file. Its a lot simpler. Commented Feb 27, 2012 at 17:06
  • if you want text without the tags, you can use String.replace with a regex or something Commented Feb 27, 2012 at 17:08
  • I'm not sure of what you mean. strings.xml from android SDK? this is a third-party file I need to parse Commented Feb 27, 2012 at 17:10
  • Yes but the tag item is different in some cases has attributes and so on.. The real file is wide more complicated than this snippet. Commented Feb 27, 2012 at 17:14
  • stackoverflow.com/a/16069754/236743 Commented May 27, 2014 at 17:34

2 Answers 2

13

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();   
        }
    } 
}
Sign up to request clarification or add additional context in comments.

1 Comment

It worked! Not exactly like this because of my XML, but enough to put me on my own feet. tx a lot
-4

Have you had a look at the Android documentation for XMLPullParser? It pretty easy to follow. Let us know if that solves your problem.

2 Comments

I did and also xmlpullparser website. Didn't find about nested tags.
@MikeD Negative. That documentation is not easy to follow for nested xml tags.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.