Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

XMLPullParser


Android recommends to use XMLPullParser to parse the xml file than SAX and DOM because it is fast.

The org.xmlpull.v1.XmlPullParser interface provides the functionality to parse the XML document using XMLPullParser.

Events of XmlPullParser

The next() method of XMLPullParser moves the cursor pointer to the next event. Generally, we use four constants (works as the event) 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 available


Example of android XMLPullParser

activity_main.xml

Drag the one listview from the pallete. Now the activity_main.xml file will look like this:

File: activity_main.xml
  1. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     tools:context=“.MainActivity” >
  6.     <ListView
  7.         android:id=“@+id/listView1”
  8.         android:layout_width=“match_parent”
  9.         android:layout_height=“wrap_content” >
  10.     </ListView>
  11. </RelativeLayout>

xml document

Create an xml file named employees.xml inside the assets directory of your project.

File: employees.xml
  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <employees>
  3.     <employee>
  4.         <id>1</id>
  5.         <name>Sachin</name>
  6.         <salary>50000</salary>
  7.     </employee>
  8.     <employee>
  9.         <id>2</id>
  10.         <name>Nikhil</name>
  11.         <salary>60000</salary>
  12.     </employee>
  13. </employees>

Employee class

Now create the Employee class that corresponds to the xml file.

File: Employee.java
  1. package com.example.xmlpullparsing;
  2. public class Employee {
  3.      private int id;
  4.      private String name;
  5.      private float salary;
  6.         public int getId() {
  7.         return id;
  8.     }
  9.     public void setId(int id) {
  10.         this.id = id;
  11.     }
  12.     public String getName() {
  13.         return name;
  14.     }
  15.     public void setName(String name) {
  16.         this.name = name;
  17.     }
  18.     public float getSalary() {
  19.         return salary;
  20.     }
  21.     public void setSalary(float salary) {
  22.         this.salary = salary;
  23.     }
  24.     @Override
  25.     public String toString() {
  26.         return ” Id= “+id + “\n Name= “ + name + “\n Salary= “ + salary;
  27.     }
  28. }

XMLPullParserHandler class

Now write the code to parse the xml file using XMLPullParser. Here, we are returning all the employee in list.

File: XMLPullParserHandler.java
  1. package com.example.xmlpullparsing;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import org.xmlpull.v1.XmlPullParser;
  7. import org.xmlpull.v1.XmlPullParserException;
  8. import org.xmlpull.v1.XmlPullParserFactory;
  9. public class XmlPullParserHandler {
  10.     private List<Employee> employees= new ArrayList<Employee>();
  11.     private Employee employee;
  12.     private String text;
  13.     public List<Employee> getEmployees() {
  14.         return employees;
  15.     }
  16.     public List<Employee> parse(InputStream is) {
  17.            try {
  18.             XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
  19.             factory.setNamespaceAware(true);
  20.             XmlPullParser  parser = factory.newPullParser();
  21.             parser.setInput(is, null);
  22.             int eventType = parser.getEventType();
  23.             while (eventType != XmlPullParser.END_DOCUMENT) {
  24.                 String tagname = parser.getName();
  25.                 switch (eventType) {
  26.                 case XmlPullParser.START_TAG:
  27.                     if (tagname.equalsIgnoreCase(“employee”)) {
  28.                         // create a new instance of employee
  29.                         employee = new Employee();
  30.                     }
  31.                     break;
  32.                 case XmlPullParser.TEXT:
  33.                     text = parser.getText();
  34.                     break;
  35.                 case XmlPullParser.END_TAG:
  36.                     if (tagname.equalsIgnoreCase(“employee”)) {
  37.                         // add employee object to list
  38.                         employees.add(employee);
  39.                     }else if (tagname.equalsIgnoreCase(“id”)) {
  40.                         employee.setId(Integer.parseInt(text));
  41.                     }  else if (tagname.equalsIgnoreCase(“name”)) {
  42.                         employee.setName(text);
  43.                     } else if (tagname.equalsIgnoreCase(“salary”)) {
  44.                         employee.setSalary(Float.parseFloat(text));
  45.                     }
  46.                     break;
  47.                 default:
  48.                     break;
  49.                 }
  50.                 eventType = parser.next();
  51.             }
  52.         } catch (XmlPullParserException e) {e.printStackTrace();}
  53.         catch (IOException e) {e.printStackTrace();}
  54.         return employees;
  55.     }
  56. }

MainActivity class

Now, write the code to display the list data in the ListView.

File: MainActivity.java
  1. package com.example.xmlpullparsing;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.List;
  5. import android.os.Bundle;
  6. import android.app.Activity;
  7. import android.view.Menu;
  8. import android.widget.ArrayAdapter;
  9. import android.widget.ListView;
  10. public class MainActivity extends Activity {
  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.        ListView listView = (ListView) findViewById(R.id.listView1);
  16.         List<Employee> employees = null;
  17.         try {
  18.             XmlPullParserHandler parser = new XmlPullParserHandler();
  19.             InputStream is=getAssets().open(“employees.xml”);
  20.             employees = parser.parse(is);
  21.             ArrayAdapter<Employee> adapter =new ArrayAdapter<Employee>
  22.     (this,android.R.layout.simple_list_item_1, employees);
  23.             listView.setAdapter(adapter);
  24.         } catch (IOException e) {e.printStackTrace();}
  25.     }
  26.     @Override
  27.     public boolean onCreateOptionsMenu(Menu menu) {
  28.         // Inflate the menu; this adds items to the action bar if it is present.
  29.         getMenuInflater().inflate(R.menu.activity_main, menu);
  30.         return true;
  31.     }
  32. }

Output:

xmlpullparsing

Comments

comments