Home > Tutorials > Android XML Adventure – Parsing XML Data with XmlPullParser

Android XML Adventure – Parsing XML Data with XmlPullParser


Article Series: Android XML Adventure

Author: Pete Houston (aka. `xjaphx`)

TABLE OF CONTENTS

  1. What is the “Thing” called XML?
  2. Parsing XML Data w/ SAXParser
  3. Parsing XML Data w/ DOMParser
  4. Parsing XML Data w/ XMLPullParser
  5. Create & Write XML Data
  6. Compare: XML Parsers
  7. Parsing XML using XPath
  8. Parsing HTML using HtmlCleaner
  9. Parsing HTML using JSoup
  10. Sample Project 1: RSS Parser – using SAXParser
  11. Sample Project 1: RSS Parser – using DOM Parser
  12. Sample Project 1: RSS Parser – using XMLPullParser
  13. Sample Project 2: HTML Parser – using HtmlCleaner
  14. Sample Project 2: HTML Parser – using JSoup
  15. Finalization on the “Thing” called XML!

=========================================

The last XML parser, I’d like to introduce to you today is `XmlPullParser`.

The concept is `XmlPullParser` is similar to the `SAXParser`; however, instead of using such callback methods for handling objects like `startDocument()`, `startElement()`, `endElement()` … in `SAXParser`, `XmlPullParser` prefers to order the process as event states.

There are five event states in `XmlPullParser`:

  1. START_DOCUMENT: the pointer of data is current at the beginning of XML document. It’s equivalent to `startDocument()` .
  2. END_DOCUMENT: already reach the end of XML document. It’s equivalent to `endDocument()`.
  3. START_TAG: at the beginning of a tag. It’s equivalent to `startElement()`.
  4. END_TAG: at the end of a tag. It’s equivalent to `endElement()`.
  5. TEXT: pointer is at the content text of a tag. It’s equivalent to `characters()`.

As you see, by using this, you can just parse whole XML document in a loop by jumping to each XML tag on every turn.

Let’s head to a simple example with `XmlPullParser`, still using the template XML and output from previous articles.

package pete.android.study.parser;

import java.io.IOException;
import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import pete.android.study.data.Study;

public class StudyParser {
	public static Study parse(InputStream is) {
		// create new Study object to hold data
		Study study = null;

		try {
			// get a new XmlPullParser object from Factory
			XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
			// set input source
			parser.setInput(is, null);
			// get event type
			int eventType = parser.getEventType();
			// process tag while not reaching the end of document
			while(eventType != XmlPullParser.END_DOCUMENT) {
				switch(eventType) {
					// at start of document: START_DOCUMENT
					case XmlPullParser.START_DOCUMENT:
						study = new Study();
						break;

					// at start of a tag: START_TAG
					case XmlPullParser.START_TAG:
						// get tag name
						String tagName = parser.getName();
						// if <study>, get attribute: 'id'
						if(tagName.equalsIgnoreCase(Study.STUDY)) {
							study.mId = Integer.parseInt(parser.getAttributeValue(null, Study.ID));
						}
						// if <content>
						else if(tagName.equalsIgnoreCase(Study.CONTENT)) {
							study.mContent = parser.nextText();
						}
						// if <topic>
						else if(tagName.equalsIgnoreCase(Study.TOPIC)) {
							study.mTopic = parser.nextText();
						}
						// if <author>
						else if(tagName.equalsIgnoreCase(Study.AUTHOR)) {
							study.mAuthor = parser.nextText();
						}
						// if <date>
						else if(tagName.equalsIgnoreCase(Study.DATE)) {
							study.mDate = parser.nextText();
						}
						break;
				}
				// jump to next event
				eventType = parser.next();
			}
		// exception stuffs
		} catch (XmlPullParserException e) {
			study = null;
		} catch (IOException e) {
			study = null;
		}

		// return Study object
		return study;
	}
}

- Use: `getEventType()` to get the current event state.

- To get attribute, use `getAttributeValue()`.

- To get text content of current tag, use `nextText()`. Why is it? It’s because if current pointer is at a tag name, `<content>`, then the next element is a tag text, which is the content inside between open-close tag; hence, the event END_TAG will be `</content>`.

- To move to next XML tag, use `next()`.
Well, hope you learn something. See you in next article!

Cheers,

Pete Houston

About these ads
  1. Avinash Jadhav
    July 19, 2013 at 1:34 pm

    Hi,

    You know before visiting this article I was so much confused with XmlPullParser also with the other parsers like SAX, DOM, etc. But I after reading the code I automatically got the process and come to know some core concepts.
    Thanks for such a great article.

    But if you could help me on parsing xml or json API’s with Async concept then It will be more help full for me….

  2. April 19, 2013 at 6:54 pm

    little problem with the tags html, so xmlData: -start game – start cel – 5 – end cel – start val – 2 end val – end game….

  3. April 19, 2013 at 6:52 pm

    arturohm2013 :
    conrrection for string xmlData : “52″

  4. April 19, 2013 at 6:50 pm

    conrrection for string xmlData : “52”

  5. April 19, 2013 at 6:40 pm

    Hi,
    i’m newbie in android, i’m trying to parse an string with xml data but i have problems to understand the process, please bit help, here my code :

    string xmlData = “52”;

    private void getXML(InputStream xmlData){
    String celda = “”;
    String valor = “”;
    //Study study = null;
    try{
    XmlPullParser xpp = Xml.newPullParser();
    xpp.setInput(xmlData,”UTF-8″);
    //xpp.nextTag();
    int eventType = xpp.getEventType();

    while (eventType != XmlPullParser.END_DOCUMENT){
    switch(eventType){
    case XmlPullParser.START_DOCUMENT:
    //
    break;

    case XmlPullParser.START_TAG:
    String tagName = xpp.getName();
    if(tagName.equalsIgnoreCase(“cell”)){
    celda = xpp.getText();
    } else if (tagName.equalsIgnoreCase(“val”)){
    valor = xpp.getText();
    }
    }
    }
    }
    catch (XmlPullParserException e)
    { Log.e(“getXML”,e.getMessage()); }
    return;

    what’s wrong ? i give a parameter and i want to have 2 (cell=5 and val = 2) and show them in 2 editText.

    Thanks

  6. Amino
    June 15, 2012 at 2:55 pm

    Hello there,

    I am getting this error message when running it on android 4
    org.xml.sax.SAXParseException: Unexpected token (position:TEXT @1:2 in java.io.StringReader@41369600)
    Could u say what I am missing?

  7. April 13, 2012 at 5:33 am

    This sir, is the best parser tutorial I have got on hand.

    If it’s a normal project that people normally take existing xml from R.xml.study I would recommend also listing the alternative:
    XmlPullParser parser = getResources().getXml(R.xml.study);
    and then start from there.

    Thank you for the useful information and keep the good work up man!

  8. tejas
    February 7, 2012 at 5:21 pm

    plz share zip file..

  9. January 20, 2012 at 4:31 am

    Hi,

    I’m trying to parse an XML feed following this post: http://androidresearch.wordpress.com/2012/01/17/parsing-xml-with-xmlpullparser-2/, but I don’t much difference from your post.

    And my problem is that I’m looking for the <title> tag, but my code parses and tags like this: <media:title type=”html”> too, and this is not my expected result.
    I want only tags like this <title> to be parsed.

    Could you help me?
    I said that the post I’m following is similar with yours, and most probably your code also has this issue.

  10. Ajay
    December 29, 2011 at 8:37 pm

    Hi Pete,
    I really enjoyed the way you represented the code this all required summary. But want to suggest for more effectiveness please share the simple project in zip. Will be much easier to work with.

    Thanks,

    Ajay

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: