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.

android_rss

XmlPullParser

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 http://developer.android.com/intl/vi/reference/org/xmlpull/v1/XmlPullParser.html.

import java.io.IOException;
 import java.io.StringReader;

 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();
         factory.setNamespaceAware(true);
         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 = xpp.next();
         }
         System.out.println("End document");
     }
 }
 

DefaultHandler

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

Example:

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

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;
    }

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

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

    @Override
    public String getStreamResult() {
        return elementFinder.getResultOrThrow();
    }
}

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

Leave a Comment

Your email address will not be published. Required fields are marked *

We care about your privacy and data security. Google serves cookies to analyse traffic and show ads on this site. By using this site you agree to these cookies being set. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close