Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From \$16.50/m

# Android SDK: Build a Simple SAX Parser

Parsing data from an XML file is a very common goal in mobile applications. This tutorial will provide you with a hands on approach for reading XML data with a SAX parser. SAX is an abbreviation for "Simple API for XML", and it is a very powerful tool for reading XML.

## SAX Parser Pros and Cons

One of the biggest advantages that SAX parsers offer is a low memory footprint. They parse each line of XML data at a time and consequently do not need to load the entire XML document into memory prior to making the data accessible. This is a significant boost to performance, and this really becomes visible when working with large XML documents.

One of the disadvantages of using a SAX parser is that you must define an event-driven API that will respond to each element as it is received. This can become time consuming to build, but if you are willing to spend a little extra time to get it right, the outcome will be worthwhile.

## Understanding XML

XML is a means of storing and transporting data. One of the main things you will need to know for this tutorial is the structure of an XML document. XML is made from a series of tags, just like HTML. An example of am opening tag would be <example>, and a complementary closing tag would be </example>. Between these tags we will find the data contained by the example tag (also called an element). In certain cases, we may have tags with attributes that we need to handle in our SAX parser. An example of this occurring in XML would be <example attr='value'>.

## Step 1: Setting Up the Application

You will need to create a new project in Eclipse. Since we are working with XML and will be making use of the Internet for data transfer, we need to give the application permission to access the Internet. To do this you need to open the application's manifest file and add this line of code at the bottom, just before the closing </manifest> tag:

While you are in your manifest file you should also make your application debuggable. This can be done by adding the following line of code in your application tag:

Now that the application has access to the Internet and is debuggable, we can begin implementing the SAX parser.

## Step 2: Creating Classes and UI

First, you will need to create three classes. The first class that should be created when you created the project will be your activity. In this activity, all the data retrieved will be used and displayed to the screen. The second class required will be the handler, in this class all the extracting and setting of data will be done. The third class will contain all the getters and setters that we need to set the data and retrieve it.

The main.xml in your resource folder should be modified as shown below:

## Step 3: The Content Handler

The content handler is where we need to handle each of the incoming events from the XML. So, basically what the content handler will do is read through the XML until it reaches the end.

When an opening tag such as <example> is reached, the startElement handler will be called. When closing tags such as </example> is reached, a closing method called endElement gets called. When the SAX parser reaches the closing tags, it calls a method called characters which will get all the content that is between the opening and closing tags.
We have a String that is called elementValue, which is set to null. Each time the parser goes through to start an end tag, we will set the elementValue string to the data in between those tags.
We will also make use of a Boolean value called elementOn. We use this to keep track of where the XML is being passed, so that when a tag is finished being read and data has been extracted, we set it to false so that we can read the next tag in the XML.

We have if statements to check when we reach a specific tag in order to use setters to set the data to an array list, so that we can display it later.

Below is the sample code used in the content handler:

In the startElement I have included a comment to show how to retrieve the attribute of an opened tag.

## Step 4: Getters, Setters, and Logcat

This is a very simple class which contains arraylists, getters and setters. Here the array lists are set from the content handler class by calling their respective setter methods.

In this example, it is simple because we are basically retrieving string types. However, in other XML files you may need to retrieve other types, such as date or URL, and you will need to make adjustments as needed. Take date for example: you might have to use a date formatter to format the date correctly before you set it.

In each of the setter methods I have added logs like the one below:

This is very useful as you can use the Logcat that is located in the DDMS perspective to track all the data that you are retrieving for the XML. A great advantage of doing this is that if your code fails, you will know exactly at which tag the parser is failing and can make the necessary adjustments.

Below is a snippet of code from the getters and setters class showing just one of the getter and setter methods:

Below is an image of Logcat showing all the retrieved data.

## Step 5: Displaying the Retrieved Data

In the activity, we will need to create an instance of the SAX parser. We also need to create the handler for each of the XML tags. We also have the URL of the XML that we need to pass into the Handler. Here is the code used to implement this:

The final step is to display the data to an Activity. For each data category we need to create a different TextView array. For example:

By doing this, we will be able to add all the retrieved data to these TextViews. We need to make the length of the TextView the size of the TextView, the following line of code will set the TextView length to the size of the TextView array:

We need to get the layout of the Activity in order to set all the TextViews that are created for it programmatically. Using a view we can do this simply. Below is the line of code we require:

Now that we have the length of the TextView, we can simply run a for loop to add the data to these TextViews. The for loop will end when the end of the TextView array is reached.

Here is the code used to implement the for loop to set the data to TextViews and then set the TextViews to the layout:

The last line of code would be used to set the layout:

## Conclusion

Now that everything is set up properly, you can run your app. The results should look like the image below. If you do get an error, the most likely reason would be that you are not handling the XML correctly. The best way to approach debugging that is by using the Logcat.