XML pull parser reading asset

This is my example pulled from a few sources (probably stackoverflow but I no longer have the links).  Put employees.xml in assets folder of project.

<?xml version="1.0" encoding="UTF-8"?>
<employees>
    <employee>
        <id>2163</id>
        <name>Kumar</name>
        <department>Development</department>
        <type>Permanent</type>
        <email>kumar@tot.com</email>
    </employee>
    <employee>
        <id>6752</id>
        <name>Siva</name>
        <department>DB</department>
        <type>Contract</type>
        <email>siva@tot.com</email>
    </employee>
    <employee>
        <id>6763</id>
        <name>Timmy</name>
        <department>Testing</department>
        <type>Permanent</type>
        <email>timmy@tot.com</email>
    </employee>
</employees>

Create class Employee.

public class Employee {

	private String name;
    private int id;
    private String department;
    private String type;
    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return id + ": " + name + "\n" + department + "-" + type
                + "\n" + email;
    }
}

Layout main.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".XMLPullParserActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/hello_world"
        android:textColor="#CC0033"
        android:textSize="16sp" />

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/text" />

</RelativeLayout>

Activity.

public class XMLPullParserActivity extends Activity {
	ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

        listView = (ListView) findViewById(R.id.list);

        List employees = null;
        try {
            XMLPullParserHandler parser = new XMLPullParserHandler();
            employees = parser.parse(getAssets().open("employees.xml"));
            ArrayAdapter adapter =
                new ArrayAdapter(this,R.layout.list_item, employees);
            listView.setAdapter(adapter);
        } catch (IOException e) {
            e.printStackTrace();
        }
	}
}

Create class XMLPullParserHandler.

public class XMLPullParserHandler {

	List employees;
    private Employee employee;
    private String text;

    public XMLPullParserHandler() {
        employees = new ArrayList();
    }

    public List getEmployees() {
        return employees;
    }

    public List parse(InputStream is) {
        XmlPullParserFactory factory = null;
        XmlPullParser parser = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            parser = factory.newPullParser();

            parser.setInput(is, null);

            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagname = parser.getName();
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tagname.equalsIgnoreCase("employee")) {
                        // create a new instance of employee
                        employee = new Employee();
                    }
                    break;

                case XmlPullParser.TEXT:
                    text = parser.getText();
                    break;

                case XmlPullParser.END_TAG:
                    if (tagname.equalsIgnoreCase("employee")) {
                        // add employee object to list
                        employees.add(employee);
                    } else if (tagname.equalsIgnoreCase("name")) {
                        employee.setName(text);
                    } else if (tagname.equalsIgnoreCase("id")) {
                        employee.setId(Integer.parseInt(text));
                    } else if (tagname.equalsIgnoreCase("department")) {
                        employee.setDepartment(text);
                    } else if (tagname.equalsIgnoreCase("email")) {
                        employee.setEmail(text);
                    } else if (tagname.equalsIgnoreCase("type")) {
                        employee.setType(text);
                    }
                    break;

                default:
                    break;
                }
                eventType = parser.next();
            }

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return employees;
    }
}

And the other layout (list_item) to go into the listview.

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" />

 

This entry was posted in Android. Bookmark the permalink.

Leave a Reply

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