Android XML Parsing Tutorial - Using SAXParser

Android XML Parsing Tutorial - Using DOMParser
Here we are going to see about how to parse a XML using SAX Parser.
we are going to parse XML from net not from local file.
Files Used:- ( Main Activity ) ( Getter & Setter Method ) ( XML Handling )
example.xml ( XML file from net )
The output will looks similar to


example.xml ( )
[sourcecode language="xml"]
<website category="android"></website>
<website category="iPhone"></website>
If tag names are different, then we can use string to set & get the value. But here item,name & website tags are repeating 2 times.
So we can use ArrayList to store & get the data.
This is main activity class. when App. starts this file will be called first.
This file contains how to use SAX Parser to handle XML tags.
[sourcecode language="java"]
package com.androidpeople.xml.parsing;

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.widget.LinearLayout;
import android.widget.TextView;

public class XMLParsingExample extends Activity {

/** Create Object For SiteList Class */
SitesList sitesList = null;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {

/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);

/** Create a new textview array to display the results */
TextView name[];
TextView website[];
TextView category[];

try {

/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();

/** Send URL to parse XML Tags */
URL sourceUrl = new URL(

/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.parse(new InputSource(sourceUrl.openStream()));

} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);

/** Get result from MyXMLHandler SitlesList Object */
sitesList = MyXMLHandler.sitesList;

/** Assign textview array lenght by arraylist size */
name = new TextView[sitesList.getName().size()];
website = new TextView[sitesList.getName().size()];
category = new TextView[sitesList.getName().size()];

/** Set the result text in textview and add it to layout */
for (int i = 0; i < sitesList.getName().size(); i++) {
name[i] = new TextView(this);
name[i].setText("Name = "+sitesList.getName().get(i));
website[i] = new TextView(this);
website[i].setText("Website = "+sitesList.getWebsite().get(i));
category[i] = new TextView(this);
category[i].setText("Website Category = "+sitesList.getCategory().get(i));


/** Set the layout view to display */

This file is used to handle the XML tags. So we need to extends with DefaultHandler.
we need to override startElement, endElement & characters method .
startElemnt method called when the tag starts.
endElemnt method called when the tag ends
characres method to get characters inside tag.
[sourcecode language="java"]
package com.androidpeople.xml.parsing;

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

public class MyXMLHandler extends DefaultHandler {

Boolean currentElement = false;
String currentValue = null;
public static SitesList sitesList = null;

public static SitesList getSitesList() {
return sitesList;

public static void setSitesList(SitesList sitesList) {
MyXMLHandler.sitesList = sitesList;

/** Called when tag starts ( ex:- <name>AndroidPeople</name>
* -- <name> )*/
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {

currentElement = true;

if (localName.equals("maintag"))
/** Start */
sitesList = new SitesList();
} else if (localName.equals("website")) {
/** Get attribute value */
String attr = attributes.getValue("category");


/** Called when tag closing ( ex:- <name>AndroidPeople</name>
* -- </name> )*/
public void endElement(String uri, String localName, String qName)
throws SAXException {

currentElement = false;

/** set value */
if (localName.equalsIgnoreCase("name"))
else if (localName.equalsIgnoreCase("website"))


/** Called to get tag characters ( ex:- <name>AndroidPeople</name>
* -- to get AndroidPeople Character ) */
public void characters(char[] ch, int start, int length)
throws SAXException {

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



Contains Getter & Setter Method
[sourcecode language="java"]
package com.androidpeople.xml.parsing;

import java.util.ArrayList;

/** Contains getter and setter method for varialbles */
public class SitesList {

/** Variables */
private ArrayList<String> name = new ArrayList<String>();
private ArrayList<String> website = new ArrayList<String>();
private ArrayList<String> category = new ArrayList<String>();

/** In Setter method default it will return arraylist
* change that to add */

public ArrayList<String> getName() {
return name;

public void setName(String name) {;

public ArrayList<String> getWebsite() {
return website;

public void setWebsite(String website) {;

public ArrayList<String> getCategory() {
return category;

public void setCategory(String category) {


That's it. :)
You can download the full source code from here
Android XML Parsing Tutorial - Using DOMParser


Jekho, can you briefly tell

can you briefly tell your question ?..



google it.

google it.

Hi Caroline, To parse the

Hi Caroline,
To parse the image tag from xml see this example

To download images from web see this example

Hi Caroline, can you paste

Hi Caroline,
can you paste your logcat error here.?
So that we can try to solve the problem

@sindhu tiwari, I'm not clear

@sindhu tiwari,
I'm not clear with the question. make it clear.

@sindhu, I'm not sure of the


I'm not sure of the tags that are used in your xml fle. So, please give a sample file. So that we can help you..

hi catalist,

hi catalist,
i dont get ur question.. make it clear plz.

@hamada, I think the problem


I think the problem arrives from . Can you post your file code ?. So that we try to solve it.

@hamada, I think the


I think the file code is right. May be you done some thing wrong in parsing file.( ). check it..

@sathishbabu, can you please


can you please paste your logcat error ?

So that we can try to find the error.

@sathish, Paste your logcat


Paste your logcat error message

@sathish, Can't able to find


Can't able to find error in your logcat. try to download one more time
and import the project in eclipse and run it.