Home Tutorials Hints & Tips

Getting Started in Android Development

Part 32 - Android XML Parsing

Many application program interfaces or APIs are of XML format. XML stands for Extensible Markup Language. By now you should be familiar with XML as it is used to define Android layouts. However, being a flexible, well defined language, it is often used as an API format. Typical uses would be to hold the data of a product catalogue, weather data, personnel records or the like. It's important to point out that when dealing with XML data, we are typically reading or parsing existing data rather than modifying or creating the data.

Basic rules for XML

XML files generally start with a prologue. If the file has a prologue is must always be the first line and is typically:

<?xml version="1.0" encoding="UTF-8"?>

XML documents must contain one root element that is the parent of all other elements:


All XML elements must have a closing tag in either of the two formats below:

<p>This is a paragraph</p>
<br />

XML Tags are case sensitive

XML elements must be properly nested

<b><i>This text is bold and italic</i></b>

XML attribute values must be quoted

<note date="10/09/2012">

Entity references

Some characters have a special meaning in XML. Generally these would produce errors when being parsed. However, they can be replaced with entity references.

There are 5 pre-defined entity references in XML:

Comments in XML

<!-- This is a comment -->

Well formed XML

XML documents that conform to the syntax rules above are said to be "well formed" XML documents.

XML Parsing Options

There are basically three forms of XML parser supported in Android. These are DOM, SAX and Pull. There are basic differences between them and there are guidelines as to which may be more suitable based on the circumstances. DOM validates the file, so is a good choice when validation is needed. However, the validation can be compromized if the document is too large. SAX loads the entire file into memory before processing. It's a good choice for larger files and files that require the whole file parsing. If the file is small and you only want to parse part of the file then go for the Pull parser. These are only guidelines and not hard and fast rules as it is hard to define what a large or small file is.

Our XML Example

I have chosen to use the XmlResourceParser class over the XmlPullParser Class. The XmlResourceParser is a standard pull parser with some extensions. In our example they could be interchanged.

The first task is to create an XML file to hold some example data. Our file with some weather data is saved under a dedicated folder called "xml" under the resources folder.

The weather.xml data file is shown below.

The first task is to create an object of the XmlResourceParser that points to the xml file weather.xml.

XmlResourceParser parser = getResources().getXml(R.xml.weather);

A private method is then called to process the data in the XML file. Android Studio will force you to surround the method with a "try and catch". This stops the apps crashing should the file not be there or it be malformed.

The basic function of the procesData() method is to parse the XML file and set the values of the city, temperature and weather attributes to string values.

The first task is the set the "eventType" to a value such as -1 that is not used by the methods in the XmlResourcePasrer class. You can picture the eventType as a file pointer incrementing thought the XML file as it is parsed. The value of -1 forces the code into the while loop.

The while loop executes until the end of the XML file.

while (eventType != XmlResourceParser.END_DOCUMENT)

An if statement then looks to see if the eventType is an XML start tag.

if (eventType == XmlResourceParser.START_TAG)

The next block is code gets the name of the start tag and if it is "location" as per our XML example file, then the values of the attributes are retrieved and stored as strings. This process is repeated for each start tag. There is another private method call printValues() that is called in this block of code. This will print the string values. This method is covered in the next tutorial.

String locationValue = parser.getName();
   if (locationValue.equals("location")) {
      String city = parser.getAttributeValue(null, "city");
      String temperature = parser.getAttributeValue(null, "temperature");
      String weather = parser.getAttributeValue(null, "weather");
      printValues (city, temperature, weather);

Finally, we need to get the next eventType and if it is the end of file then the while loop terminates.

The testing of the parser is covered in the next tutorial.

After this tutorial your XmlParserActivity.java file should look similar to the one below:

Your activity_xml_parser.xml file should look similar to the one below:

Your MainActivity.java file should look similar to the one below:

Your AndroidManifest file should look similar to the one below:

Download Download tutorial set 2

Privacy and Cookies Disclaimer Copyright
© 2015 - 2018 North Border Tech Training All rights reserved