XML Resources in Android, using XmlResourceParser

Well i’m kinda new to android and i’m learning everything that i ca find about android app development so just yesterday i realised that i havent written any post in my blog lately so why not write about android??? well now i’m writing everything that i learn from no onwards as far as possible. well if i have a time i will do post it for sure.

So today lets start with XML parsing using XmlResourceParser. so i’ve divided it in steps and here it goes:

STEP 1. CREATE A LAYOUT WITH XML
Create an Xml file named sample.xml in a folder named xml(you can name your xml filename and folder name as you like for me its res/xml/sample.xml)

CODE:
=================================================================

<?xml version=”1.0″ encoding=”UTF-8″?>
<school>
    <class1>beginners</class1>
    <class2>beginners+</class2>
    <class9>Young</class9>
    <class12>Adult</class12>
</school>

=================================================================

well the getDepth() or the counting of XML is like this:

 <!– outside –>     0
 <root>                  1
   sometext                 1
     <foobar>         2
     </foobar>        2
 </root>              1
 <!– outside –>     0
=================================
Just dont get confused you will be clear soon.

STEP 2. NOW CREATE A LAYOUT
Now We need to create a Layout where we want to display our output. For this i have just created a header and a TextView.

CODE:
=================================================================

<TextView
        android:id=”@+id/first”
        android:layout_width=”fill_parent”
        android:layout_height=”wrap_content”
        android:text=”@string/hello_world” />

<TextView
android:id=”@+id/second”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_below=”@+id/first” />

=================================================================

STEP 3: Write code in your MainActivity.java file
=================================================================

package com.ashim.xmlparseexample_1;

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

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView displayXmlHere =(TextView)findViewById(R.id.second); //make TextView Reference as displayXmlHere
String storeHere; //make a string reference as storeHere
try{
storeHere=fromAnXml(this); //call a method fromAnXml and store everything returned to storeHere
displayXmlHere.setText(storeHere); //display everything that is in storeHere
}catch(XmlPullParserException e){ //if an Xml pulls some Parser exception
e.printStackTrace();
}catch(IOException e){ //if an IO exception shows up
e.printStackTrace();
}
}
public String fromAnXml(Activity a) throws XmlPullParserException, IOException{
StringBuffer buffer = new StringBuffer(); //buffer as a stringBuffer
Resources res =a.getResources(); //Return a Resources instance for your application’s package.
XmlResourceParser xpp =res.getXml(R.xml.sample); //Return an XmlResourceParser for the given resource ID.
xpp.next(); //Get next parsing event

int EventType = xpp.getEventType(); //returns the type of the current Events
System.out.println(“TOTAL EventType:”+EventType); //This Prints the initial value of EventType. We can see it in the LogCat to know how it works

while(EventType !=XmlPullParser.END_DOCUMENT){//check if the value of EventType is equal or not with The END_DOCUMENT if not enter inside the loop
if(EventType==XmlPullParser.START_DOCUMENT)
{
buffer.append(“START OF AN XMLn”);
}
if(EventType == XmlPullParser.START_TAG){
buffer.append(“nSTART_TAG: “+xpp.getName());
}
if(EventType == XmlPullParser.END_TAG){
buffer.append(“nEND_TAG: “+xpp.getName());
}
if(EventType ==XmlPullParser.TEXT){
buffer.append(“nText: “+xpp.getText());
}
System.out.println(“VALUE OF EventType:”+EventType);
EventType=xpp.next();
}
buffer.append(“nnEND OF AN XML”);
return buffer.toString(); //returns the StringBuffer i.e “buffer” to the above calling method
}

}

=================================================================
So This is IT. just try it once if u face any problem just comment below or ask Google Uncle. 😛