This tutorial is about XML parsing in android. We will be using SAX parser to implement android XML parser example. We will be using AsyncTask to get the xml in the background thread. Android provides various methods to parse XML document like XmlPullParser, SAXParser, DOMParser but we will be using the SAX Parser since our tutorial aims to reduce the overhead and simplify the process and not really increase it. This tutorial is structured as following:

  1. Getting the xml feed from given URL in the Background thread using Asynctask.
  2. Parsing the XML feed.
  3. Using the parsed data to update the TextView which we will render in our small app.

Make sure you include internet permission in manifest file if you are using internet to parse xml file.

XML file or feed

Our android xml parser application will process the xml file given below. This XML file contains structured information about different books. You can parse Book Number, Price, Description and rating.

Below you can find the screenshot of xml parsing application.


On specifying the URL of XML file you can fetch the data.


Implementing the Android XML Parser

Our XML Parser implementation is divided into 4 classes.

  1. It renders the TextView from input URL using AsyncTask.
  2. It gets the xmlFeed by Making HttpPostRequest.
  3. It defines the class for getting and setting the individual field of one book.
  4. It does the parsing and stores in the Arraylist for Later use which makes it easier to manipulate the values in our xml feed. This class defines the structure of our xml feed. We parse information from XML file and store it inside java object.

The design paradigm is self-explanatory, please look at the structure of XML above.

Note: Since our XmlFeed is designed like above, we have defined our BookData structure like this but you have to define your own structure for manipulating the feed according to your xml design.

Request XML file

Now let us request XML file from internet so that we can parse it.

  • HTTPClient retrives the data.
  • HttpPost implies that our request is of POST type and HTTPResponse is the response received after execution.
  • URL is the dynamic string passed to this function where our XML resides.
  • We wrap this code in try catch statement so that we can bounce back from any unprecedented situation. 

XML Parsing in Android

Bookhandler class handles the parsing of XML feed. This Class uses the data structure defined in BookData class. It parses the XML which is  inputted to it through mainActivity. We fetch each book node(<BookInfo>) from XML separately and store values accordingly inside an ArrayList. It keeps on adding a node in ArrayList till it process the last tag of xml.

StartElement method processes each child node of <BookInfo> and endElement closes that node.  

Main Activity Layout

It renders the UI part of this android xml parser example.  It has an input box which takes the Input as URL of Xml feed and then process that url for potential Xml feed in the background uisng AsyncTask and then renders the parsed XML accordingly.

On clicking parse now button, the control goes to AsyncTask. We pass on the URL from input box to xmlrequest class which returns the XML as a string. This XML string is stored in response variable.

Here, we have instantiated the sax parser and necessary initialization to make our parsing work. We have define the inputstream, which is then passed on to the parser for parsing the data.

It is then parsed and stored inside arraylist through Bookhandler class. We then retrieve our arraylist and go through it iteratively. We pass it to onPostExecute function of AsynchTask. This function displays our Parsed and refined XML.

For more information regarding different xml parsing techniques you can visit the link.

That’s it For XMLParsing tutorial. Hope you liked this simple XML parsing in Android tutorial. Please write your feedback, suggestions or errors if any in the comment box below.