XMLPullParser Android

To parse the XML file, the XMLPullParser is recommended in Android. It is faster than the SAX and DOM parser. To parse the XML document using the XMLPullParser, the org.xmlpull.v1.XmlPullParser interface is used.

Events of XmlPullParser:

To move the cursor pointer to the next event, the next() method of the XMLPullParser is used. The four constants defined in the XMLPullParser interface work as the event. These are:

  • START_TAG: To read an XML start tag.
  • TEXT: To read the text content which can be retrieved using the getText() method.
  • END_TAG: To read an end tag.
  • END_DOCUMENT: To define that no more events are available.

Example of android XMLPullParser:

activity_main.xml:

In the activity_main.xml file, we will drag a Listview from the palette.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/user_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="1dp" />
</LinearLayout>

List_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="17dp" />
    <TextView
        android:id="@+id/designation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_marginTop="7dp"
        android:textColor="#343434"
        android:textSize="14dp" />
    <TextView
        android:id="@+id/location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/designation"
        android:layout_alignBottom="@+id/designation"
        android:layout_alignParentRight="true"
        android:textColor="#343434"
        android:textSize="14dp" />
</RelativeLayout>

XML document:(File: userdetails.xml)

Inside the assets directory of the project, an XML file will be created.

<?xml version="1.0" encoding="utf-8"?>
 
<users>
 
    <user>
 
        <name>A</name>
 
        <designation>Leader</designation>
 
        <loation>City1</loation>
 
    </user>
 
    <user>
 
        <name>B</name>
 
        <designation>Secretary</designation>
 
        <loation>City2</loation>
 
    </user>
 
    <user>
 
        <name>C</name>
 
        <designation>Accountant</designation>
 
        <loation>City3</loation>
 
    </user>
 
    <user>
 
        <name>D</name>
 
        <designation>Officer 1</designation>
 
        <loation>City4</loation>
 
    </user>
 
    <user>
 
        <name>E</name>
 
        <designation>Officer 2</designation>
 
        <loation>City5</loation>
 
    </user>
 
</users>

MainActivity class:(File: MainActivity.java)

In the MainActivity.java file, we will write the code to display the list data in the ListView.

package com.example.radioapp;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
 
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try{
            ArrayList<HashMap<String, String>> userList = new ArrayList<>();
            HashMap<String,String> user = new HashMap<>();
            ListView lv = (ListView) findViewById(R.id.user_list);
            InputStream istream = getAssets().open("userdetails.xml");
            XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = parserFactory.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,false);
            parser.setInput(istream,null);
            String tag = "" , text = "";
            int event = parser.getEventType();
            while (event!= XmlPullParser.END_DOCUMENT){
                tag = parser.getName();
                switch (event){
                    case XmlPullParser.START_TAG:
                        if(tag.equals("user"))
                            user = new HashMap<>();
                        break;
                    case XmlPullParser.TEXT:
                        text=parser.getText();
                        break;
                    case XmlPullParser.END_TAG:
                        switch (tag){
                            case "name": user.put("name",text);
                                break;
                            case "designation": user.put("designation",text);
                                break;
                            case "location": user.put("location",text);
                                break;
                            case "user":
                                if(user!=null)
                                    userList.add(user);
                                break;
                        }
                        break;
                }
                event = parser.next();
            }
            ListAdapter adapter = new SimpleAdapter(MainActivity.this, userList, R.layout.list_row,new String[]{"name","designation","location"}, new int[]{R.id.name, R.id.designation, R.id.location});
            lv.setAdapter(adapter);
        }
        catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
    }
}

Output:

Content Protection by DMCA.com
Please Share