Parsing XML in Android a simple example

By | December 24, 2011

This is a simple example showing how to parse an xml in Androi.
In this method the xml file is stored in res/xml folder.

First create a folder named “xml” inside the raw folder.
Then create an xml file named test.xml and copy the contents into it.

<?xml version="1.0" encoding="utf-8"?>
<root>
<name>
CoderzHeaven
</name>
<blog>
 Google
 <sub2>Android</sub2>
</blog>
</root>

This is the XML file we are going to parse.


Now the main.xml file. We place a textView to view the contents of the parsing.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"

  >
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="CoderzHeaven XML Parsing Demo"
  android:textColor="#FF0000"
  android:textStyle="bold|italic"
  android:textSize="14sp"
  android:layout_margin="10dp"
  />
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/xml_tv"
  android:layout_margin="10dp"
  />
</LinearLayout>

Now the main java file that does the parsing.
We use the “XmlPullParser” class to parse the XML

package com.coderzheaven;

import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.widget.TextView;

public class ReadXML extends Activity {
	@Override
	  public void onCreate(Bundle savedInstanceState) {
	      super.onCreate(savedInstanceState);
	      setContentView(R.layout.main);

	      TextView myXmlContent = (TextView)findViewById(R.id.xml_tv);
	      String stringXmlContent;

		  stringXmlContent = getAllXML();
		  myXmlContent.setText(stringXmlContent);

	  }

	  public String getAllXML(){

		  Activity activity = this;
		  String str = "";

		  Resources res = activity.getResources();
		  XmlResourceParser xpp = res.getXml(R.xml.test);

		  try {
				xpp.next();
				int eventType = xpp.getEventType();
				System.out.println("eventType : " + eventType);
				while (eventType != XmlPullParser.END_DOCUMENT)
			    {
					 if(eventType == XmlPullParser.START_DOCUMENT){
						 str += "nXML Parsing Starting...n";
					 }
					 else if(eventType == XmlPullParser.START_TAG)
				     {
						 str +=  "nroot tag: "+xpp.getName();
				     }
				     else if(eventType == XmlPullParser.END_TAG)
				     {
				    	 str += "nending tag: "+xpp.getName();
				     }
				     else if(eventType == XmlPullParser.TEXT)
				     {
				    	 str += "nvalue : "+xpp.getText();
				     }
				     eventType = xpp.next();
			    }
				 str += "nnXML parsing Ending......";

		  } catch (XmlPullParserException e) {
				e.printStackTrace();
		  } catch (IOException e) {
				e.printStackTrace();
		  }
		  return str;
	  }

	}

That’s all parsing is complete Go on and run the program.

Parsing XML in Android

Parsing XML in Android


Other types of XML Parsing will be covered in the following posts please be in touch

Download the fullsource from here

3 thoughts on “Parsing XML in Android a simple example

  1. Pingback: Parsing an XML from Online in Android | Coderz Heaven

  2. alek

    Hi, thanks for your article, it helped me a lot, i made modification for send the xml as string, but now i dont know how to save, retrieve and show the each values finded in differernts edittext,any suggestion ? here the code:

    public String getAllXML(){

    Activity activity = this;
    String str = “”;

    try {
    //String source
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser xpp = factory.newPullParser();
    xpp.setInput(new StringReader(xmlc));

    xpp.next();
    int eventType = xpp.getEventType();//

    while (xpp.getEventType()!=XmlPullParser.END_DOCUMENT) {
    if (xpp.getEventType()==XmlPullParser.START_TAG) {
    if (xpp.getName().equals(“cel”)) {
    str += “\ncell : “+xpp.nextText();
    dataxml.add(1, str);
    }
    if (xpp.getName().equals(“val”)) {
    str += “\nval : “+xpp.nextText();
    }
    }
    xpp.next();
    }
    } catch (XmlPullParserException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return str;
    }

    thanks in avance

    Reply
    1. James Post author

      Create a class that holds variables for the object and return that object from getAllXML() function, then set the values.
      OR
      pass the edittext objects to the above function and set the values.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *