Android Full App, Part 3: Parsing the XML response

This is the third part of the “Android Full Application Tutorial” series. The complete application aims to provide an easy way of performing movies/actors searching over the internet. In the first part of the series (“Main Activity UI”), we created the Eclipse project and set up a basic interface for the main activity of the application. In the second part (“Using the HTTP API”), we used the Apache HTTP client library in order to consume an external HTTP API and integrate the API’s searching capabilities into our application. In this part we are going to see how to parse the XML response using Android’s built-in XML parsing capabilities.

The TMDb API supports both XML and JSON formats for the HTTP responses. We are going to use XML for our application. Let’s see first how some sample responses to search queries look like:

The responses are typical XML documents that can be parsed using the standard procedures either using SAX or DOM. The SAX specification defines an event-based approach where the implemented parsers scan through XML data and they use call-back handlers whenever certain parts of the document have been reached. On the other hand, the DOM specification defines a tree-based approach to navigating an XML document.

In general, SAX’s usage is more challenging, because the API requires development of callback functions that handle the events, while the DOM approach requires a bigger memory footprint. For that reason, we are going to choose SAX for our XML parsers implementation, since our application will leave in a rather resource-constrained environment such as a mobile device.

Before we proceed with the XML parsing, we are going to create some model classes which will map the XML elements to Java classes. By simply looking at the XML responses, the following model classes can be derived:

Nothing really special here, we are just adding String fields for each XML element. Note that the Image class will be commonly used by both the Person and Movie class. Also, the Movie class provides the retrieveThumbnail method which loops through the available Images and returns the one of size “thumb” and type “poster”.

We proceed with creating a class named XmlParser which uses SAX approach in order to parse the XML responses. The class uses two custom handlers (PersonHandler and MovieHandler) in order to perform the parsing. The code for that class is the following:

In each method, we first retrieve a reference of the SAX parser factory class using the newInstance static method of the SAXParserFactory. That method returns the appropriate Android’ implementation. Then, a SAXParser object is created using the newSAXParser method, which creates a new instance of a SAXParser using the currently configured factory parameters. The SAXParser class defines the API that wraps an XMLReader implementation class. XMLReader is an interface for reading an XML document using callbacks. The callbacks are defined usually via classes that extend the DefaultHandler class, which is the default base class for SAX2 event handlers. We provide two handlers, one for parsing the person search responses (PersonHandler) and one for parsing the movies search responses (MovieHandler). The code for the PersonHandler class follows (the MovieHandler class is pretty same, thus is omitted for brevity – the source code for that class can be found in the available Eclipse project at the end of the tutorial):

The standard approach for SAX parsing (described in many tutorials online) is used, thus the above code should look familiar if you have parsed XML documents before. Note though that instead of the qName parameter, it is the localName variable that holds the element’s data.

In our class, we define the necessary callback functions:

startElement: Called when a new element is found. We initialize the appropriate field there.

endElement: Called when the element’s end has been reached. The corresponding field gets populated there.

characters: Called when new text has been found inside an element. An internal buffer gets populated with the content of the element.

Note that within a response, a number of Person elements might be found and inside each one of them, a number of Images can be found. Particularly for the images, the relevant information resides within the elements attributes and not inside a text node. Thus the appropriate getValue method is used in order to extract that information.

At this point, the third part of the series has reached its end. In this part, we prepared the infrastructure for performing the XML parsing of the API responses using the SAX approach. At the following tutorials, we will use that in order to map the responses to our model classes. You can download here the Eclipse project created so far.

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Recent Jobs

No job listings found.

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.