Android DOM Parser Example



In this example we will see how to parse an XML file using DOM Parser in Android. What we do here is load the .xml file in the assets folder and parse the content inside and show them in a text view. When compared to SAX Parser(refer : Android SAX Parser Example ) , DOM is slow and will consume a lot of memory when it loads an XML document which contains a lot of data.


Okay let us start:

1.Create an xml file (file.xml) like below

file.xml
<?xml version="1.0"?>
<food>
<item>
<name>French Toast</name>
<price>$4.50</price>
</item>
<item>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
</item>
<item>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
</item>
</food>
2. Create a new project File ->New -> Project ->Android ->Android Application Project. While creating a new project give activity name as DOMParserActivity(DOMParserActivity.java). Copy and paste the xml file inside the assets folder of the android project.

DOMParserActivity.java:
package com.aj.dom;

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class DOMParserActivity extends Activity {
 TextView tv1;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  tv1 = (TextView) findViewById(R.id.textView1);
  try {
   InputStream is = this.getAssets().open("file.xml");

   DocumentBuilderFactory dbFactory = DocumentBuilderFactory
     .newInstance();
   DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
   Document doc = dBuilder.parse(is);
   doc.getDocumentElement().normalize();
   NodeList nList = doc.getElementsByTagName("item");
   for (int temp = 0; temp < nList.getLength(); temp++) {
    Node nNode = nList.item(temp);
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
     Element eElement = (Element) nNode;
     tv1.setText(tv1.getText() + "\n\nName : "
       + getValue("name", eElement) + "\n");
     tv1.setText(tv1.getText() + "Price : "
       + getValue("price", eElement) + "\n");
     tv1.setText(tv1.getText() + "-----------------------");

    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }

 }

 private static String getValue(String sTag, Element eElement) {
  NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
    .getChildNodes();
  Node nValue = (Node) nlList.item(0);
  return nValue.getNodeValue();
 }

}

3. Now in the main.xml, add the id attribute for the TextView to be used in the DOMParserActivity


main.xml:
<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="DOM Parser Example"
        tools:context=".DOMParserActivity" />

</RelativeLayout>
4.Run the project by rightclicking project Run as → android project.

Output:

The output of this example would be similar to the one as follows: