Android parse XML file example using SAX Parser

Here we are going to see about how to parse a XML using SAX Parser. SAX parser is fast and has less memory footprint. First we need to create an instance of SAXParserFactory and then use a custom XML handler extending DefaultHandler that will map our XML file into a java bean.

Sample XML data used in this example

        <Description>ABC Item Description</Description>
        <Description>XYZ Item Description</Description>

Source code for

package com.as400samplecode;

public class ItemMaster {
    String itemNumber = null;
    String description = null;
    String price = null;
    double weight = 0;
    public String getItemNumber() {
        return itemNumber;
    public void setItemNumber(String itemNumber) {
        this.itemNumber = itemNumber;
    public String getDescription() {
        return description;
    public void setDescription(String description) {
        this.description = description;
    public String getPrice() {
        return price;
    public void setPrice(String price) {
        this.price = price;
    public double getWeight() {
        return weight;
    public void setWeight(double weight) {
        this.weight = weight;

Source code for

package com.as400samplecode;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ItemXMLHandler extends DefaultHandler {

    Boolean currentElement = false;
    String currentValue = "";
    ItemMaster item = null;
    private ArrayList<ItemMaster> itemsList = new ArrayList<ItemMaster>();

    public ArrayList<ItemMaster> getItemsList() {
        return itemsList;

    // Called when tag starts 
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        currentElement = true;
        currentValue = "";
        if (localName.equals("ItemData")) {
            item = new ItemMaster();


    // Called when tag closing 
    public void endElement(String uri, String localName, String qName)
    throws SAXException {

        currentElement = false;

        /** set value */
        if (localName.equalsIgnoreCase("ItemNumber"))
        else if (localName.equalsIgnoreCase("Description"))
        else if (localName.equalsIgnoreCase("Price"))
        else if (localName.equalsIgnoreCase("Weight"))
        else if (localName.equalsIgnoreCase("ItemData"))

    // Called to get tag characters 
    public void characters(char[] ch, int start, int length)
    throws SAXException {

        if (currentElement) {
            currentValue = currentValue +  new String(ch, start, length);



Source code for

package com.as400samplecode;

import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidParseXMLActivity extends Activity {

    private EditText xmlInput;
    private TextView xmlOutput;

    public void onCreate(Bundle savedInstanceState) {

        xmlInput = (EditText) findViewById(;
        xmlOutput = (TextView) findViewById(;

        Button parseMyXML = (Button) findViewById(;
        parseMyXML.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

    private void parseXML() {

        String parsedData = "";
        try {

            Log.w("AndroidParseXMLActivity", "Start");
            /** Handling XML */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            ItemXMLHandler myXMLHandler = new ItemXMLHandler();
            InputSource inStream = new InputSource();
            Log.w("AndroidParseXMLActivity", "Parse1");

            inStream.setCharacterStream(new StringReader(xmlInput.getText().toString()));
            Log.w("AndroidParseXMLActivity", "Parse2");

            Log.w("AndroidParseXMLActivity", "Parse3");

            ArrayList<ItemMaster> itemsList = myXMLHandler.getItemsList();
            for(int i=0;i<itemsList.size();i++){
                ItemMaster item = itemsList.get(i);
                parsedData = parsedData + "----->\n";
                parsedData = parsedData + "Item Number: " + item.getItemNumber() + "\n";
                parsedData = parsedData + "Description: " + item.getDescription() + "\n";
                parsedData = parsedData + "Price: " + item.getPrice() + "\n";
                parsedData = parsedData + "Weight: "+ item.getWeight() + "\n";
            Log.w("AndroidParseXMLActivity", "Done");
        catch (Exception e) {
            Log.w("AndroidParseXMLActivity",e );


Source code for main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" android:layout_width="fill_parent"
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello"
        android:textStyle="bold" android:textSize="25sp"
        android:paddingBottom="15dp" />
    <EditText android:layout_height="wrap_content"
        android:layout_width="match_parent" android:id="@+id/xmlInput"
        <requestFocus />
    <Button android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:id="@+id/parse"
        android:text="Parse Now" />
    <TextView android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView1" android:text="Parsed XML Data"
        android:paddingBottom="15dp" android:textStyle="bold" />
    <ScrollView android:layout_height="wrap_content"
        android:layout_width="match_parent" android:id="@+id/scrollView1">
        <LinearLayout android:id="@+id/linearLayout1"
            android:layout_width="match_parent" android:layout_height="match_parent">
            <TextView android:layout_height="wrap_content"
                android:id="@+id/xmlOutput" android:layout_width="match_parent" />

Recommended Reading


  1. hi ,

    I am bikash.Wherei put the

    ABC Item Description

    XYZ Item Description

    In the above example

  2. For demo purpose what I have done above is to create a EditText field so that any xml that you paste in there gets parsed and displayed in the TextView. In case you have a different XML file then you must write your own handler.

    xmlInput = (EditText) findViewById(;
    xmlOutput = (TextView) findViewById(;

    This is just a demo to teach Android XML sax parsing. In real life you will get your xml from some Web request or file instead of a EditText field. Please read this for if you need help with Java XML sax parsing

    Java sax parser example code

    Hope it helps

  3. Excellent work! Thanks for this...

  4. Great work brother,
    I have been searching for a good example on SAX parser and I can bet this is the best example existed.
    Thanks a lot for your commendable efforts you put in to make it so simple and understandable.

  5. thanks.

    this is one of the best sax examples!

  6. but when i use these codes , i am not able to get any output,my text view box disappears simply.

    i am not getting any error

  7. Hey Bro if u still helpin out on this code please reply.>!
    have some doubt..!
    its urgrnt

  8. I have a Local XML file which I have to parse using SAX or any other Parser.
    I have to parse URL present in <img......... src = ""
    Can you help me to code this please.
    There are 55 different URL of Images in XML file, which I have to display in list in emulator

  9. in which folder do we have to put the xml file?

    thanks a lot for this example.

  10. yeeeah guy whehre do i have to put the xml?

  11. great nice example for sax and xml parsing

  12. hi my frends this url can not open please help me