Generally,used four constants defined in the XMLPullParser interface.
- START_TAG :An XML start tag was read.
- TEXT :Text content was read; the text content can be retrieved using the getText() method.
- .END_TAG : An end tag was read.END_DOCUMENT :No more events are availabl
student.xml.
Create an Xml file named student.xml inside the assets folder.
...................................................................................................................................................................
<?xml version="1.0"?>
<records>
<student>
<id>10011223</id>
<name>Sarvesh Kaushik</name>
<fee>50000</fee>
</student>
<student>
<id>10013231</id>
<name>Varun Chauhan</name>
<fee>60000</fee>
</student>
<student>
<id>10078923</id>
<name>Shashank Singh</name>
<fee>70000</fee>
</student>
</records>
..................................................................................................................................................................
create the Student class that corresponds to the Xml file
Student.Java
...................................................................................................................................................................
package com.example.androidtestcode;
public class Student {
private int id;
private String name;
private float fee;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getFee() {
return fee;
}
public void setFee(float fee) {
this.fee = fee;
}
@Override
public String toString() {
return " Id= "+id + "\n Name= " + name + "\n Fee= " + fee;
}
}
..................................................................................................................................................................
Parse the Xml file using XMLPullParser and returning all the employee in list
PullParserHandler.Java
...................................................................................................................................................................
package com.example.androidtestcode;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
public class PullParserHandler {
private List<Student> students= new ArrayList<Student>();
private Student student;
private String text;
public List<Student> getEmployees() {
return students;
}
public List<Student> parse(InputStream isInput) {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
parser.setInput(isInput, null);
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase("student")) {
// create a new instance of employee
student = new Student();
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase("student")) {
students.add(student);
}else if (tagname.equalsIgnoreCase("id")) {
student.setId(Integer.parseInt(text));
} else if (tagname.equalsIgnoreCase("name")) {
student.setName(text);
} else if (tagname.equalsIgnoreCase("fee")) {
student.setFee(Float.parseFloat(text));
}
break;
default:
break;
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
return students;
}
}
..................................................................................................................................................................
activity_main.xml.
...................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
android:layout_marginBottom="40dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Student List"
android:gravity="center"
android:textColor="#ffffff"
android:textStyle="bold"
android:padding="7dp"
android:background="#349b2d" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="4dp" >
</ListView>
</LinearLayout>
..................................................................................................................................................................
MainActivity.Java
...................................................................................................................................................................
package com.example.androidtestcode;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
List<Student> students = null;
try {
PullParserHandler parser = new PullParserHandler();
InputStream isInput=getAssets().open("student.xml");
students = parser.parse(isInput);
ArrayAdapter<Student> adapter =new ArrayAdapter<Student>(this,android.R.layout.simple_list_item_1, students);
listView.setAdapter(adapter);
} catch (IOException e) {e.printStackTrace();}
}
}
..................................................................................................................................................................