Wednesday, December 25, 2013

XML DOM Parsing in Android.

      The Document Object Model (DOM) is an API for valid HTML and well-formed XML documents. It defines the logical structure of documents and the way of documents accessed and manipulated. It is a hierarchy-based parser that creates the object model for an entire model. It is highly recommended  for the small sized files or data because it store the data in memory. It is faster than the SAX parser.

XML Data Format:

  XML was created to structure, store and transport data. It will be easily readable by the humans as well machines. Follow the sample XML format. I am saving this data named studentDetails.xml in  res/raw folder.

<?xml version="1.0" encoding="UTF-8"?>
<students>
 <student>
  <no>1</no>
  <name>Tendulkar</name>
  <class>Computers</class>
  </student>

 <student>

  <no>2</no>
  <name>Ganguly</name>
  <class>EEE</class>
 </student>

 <student>

  <no>3</no>
  <name>Dravid</name>
  <class>CSE</class>
 </student>

 <student>

  <no>4</no>
  <name>Laxman</name>
  <class>CSIT</class>
  </student>

 <student>

  <no>5</no>
  <name>Kumble</name>
  <class>MCA</class>
  </student>

</students>



XML layout:
Create a xml layout to display the DOM results.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/NOtext"
    />
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Nametext"
    />
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Classtext"
    />
</RelativeLayout>

DomParserActivity:


package com.prakash_domparserdemo;

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 notxt,nametxt,classtxt;  
 
@Override  
public void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);  
setContentView(R.layout.domparser);  
notxt=(TextView)findViewById(R.id.NOtext); 
nametxt=(TextView)findViewById(R.id.Nametext); 
classtxt=(TextView)findViewById(R.id.Classtext); 
try {  
InputStream is = getAssets().open("details.xml");  
 
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();  
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();  
Document doc = dBuilder.parse(is);  
 
Element element=doc.getDocumentElement();  
element.normalize();  
 
NodeList nList = doc.getElementsByTagName("student");  
for (int i=0; i<nList.getLength(); i++) {  
 
Node node = nList.item(i);  
if (node.getNodeType() == Node.ELEMENT_NODE) {  
Element element2 = (Element) node;  
notxt.setText(notxt.getText()+"\nNo : " + getValue("no", element2)+"\n");  
nametxt.setText(nametxt.getText()+"Name : " + getValue("name", element2)+"\n");  
classtxt.setText(classtxt.getText()+"Class : " + getValue("class", element2)+"\n");
 
}  
}
    
} catch (Exception e) {e.printStackTrace();}  
 
}  
private static String getValue(String tag, Element element) {  
NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();  
Node node = (Node) nodeList.item(0);  
return node.getNodeValue();  
}  
 
}  


Result screen:


Enjoy Coding!!!!



Read More »