Working with Android XML

There are several standard libraries for processing XML files using Java language. The SAX and the DOM XML parsers are also available on Android which are also similar to ones used in Java.



XML Pull Parser is an interface that defines parsing functionality provided in XMLPULL V1 API. It comes with Android SDK.

The detailed usage can be found on


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

 public class SimpleXmlPullApp

     public static void main (String args[])
         throws XmlPullParserException, IOException
         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
         XmlPullParser xpp = factory.newPullParser();

         xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
         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());
          } 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 =;
         System.out.println("End document");


DefaultHandler class is available as a convenience base class for SAX2 applications. The class provides default implementations for all of the callbacks in the 4 core SAX2 handler classes: EntityResolver, DTDHandler, ContentHandler and ErrorHandler.

Following is an example:

public class XMLRSSHandler extends DefaultHandler {

	StringBuffer chars = new StringBuffer();

	public void startElement(String uri, String localName, String qName, Attributes atts) {
		chars = new StringBuffer();

	public void endElement(String uri, String localName, String qName) throws SAXException {

		if (localName.equalsIgnoreCase("title")){
			Log.e("XMLParsing", chars.toString());
		} else if (localName.equalsIgnoreCase("description")){
			Log.e("XMLParsing", chars.toString());
		} else if (localName.equalsIgnoreCase("published")){
			Log.e("XMLParsing", chars.toString());
		} else if (localName.equalsIgnoreCase("guid")){
			Log.e("XMLParsing", chars.toString());
		} else if (localName.equalsIgnoreCase("author")){
			Log.e("XMLParsing", chars.toString());
		} else if (localName.equalsIgnoreCase("content")){
			Log.e("XMLParsing", chars.toString());

	public void characters(char ch[], int start, int length) {
		chars.append(new String(ch, start, length));

External XML Parser libraries

XmlPullParser has some cons and it only works to some degree. If you need to work with complicated XML file, it is better to use an external libraries.

simple-easy-xml-parser (SEXP) is a simple and high performance parser based upon Android sax parser. SEXP gives callbacks for all parsing events and being written in pure java allows faster and more comprehensive testability.


String XML =
            "<novoda>" +
              "<favouriteColour>Blue</favouriteColour>" +

ElementFinderFactory factory = SimpleEasyXmlParser.getElementFinderFactory();
elementFinder = factory.getStringFinder();
Streamer<String> streamer = new SimpleStreamer(elementFinder);
String favouriteColour = SimpleEasyXmlParser.parse(XML, streamer);

private static class SimpleStreamer implements Streamer<String> {

    private final ElementFinder<String> elementFinder;
    private final String elementTag;

    public SimpleStreamer(ElementFinder<String> elementFinder, String elementTag) {
        this.elementFinder = elementFinder;
        this.elementTag = elementTag;

    public RootTag getRootTag() {
        return RootTag.create("novoda");

    public void stream(RootElement rootElement) {
        elementFinder.find(rootElement, elementTag);

    public String getStreamResult() {
        return elementFinder.getResultOrThrow();

Woodstox is a full-featured high-performance Java XML processor. It implements Streaming XML API, Stax and it supports all of XML.

Woodstox is gold when it comes to dealing with high volume of heavy weight xml files.