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

Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Leave a Reply

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