Android XML parsing Using DOM Parser?
In this blog parse the xml used by DOM parser. The DOM parser better then SAX parser because DOM Parser Create And Parse xml file.
parser_file.xml.
Create the file inside assets folder.
...................................................................................................................................................................
<?xml version="1.0"?>
<records>
<employee>
<name>Sarvesh Kaushik</name>
<salary>50000</salary>
</employee>
<employee>
<name>Varun Chauhan</name>
<salary>60000</salary>
</employee>
<employee>
<name>Shashank Singh</name>
<salary>70000</salary>
</employee>
</records>
..................................................................................................................................................................
main_activity.xml.
Create the file inside res/layout folder.
...................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
android:layout_marginBottom="60dp" />
<TextView
android:id="@+id/textView_parser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Details"
android:gravity="center" />
</LinearLayout>
..................................................................................................................................................................
MainActivity.java.
...................................................................................................................................................................
package com.example.androidtestcode;
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 MainActivity extends Activity {
TextView tvParser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvParser = (TextView) findViewById(R.id.textView_parser);
try {
InputStream is = getAssets().open("pasrser_file.xml");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document documentfile = docBuilder.parse(is);
Element elements=documentfile.getDocumentElement();
elements.normalize();
NodeList nodList = documentfile.getElementsByTagName("employee");
for (int i=0; i<nodList.getLength(); i++) {
Node node = nodList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element2 = (Element) node;
tvParser.setText(tvParser.getText()+"\nName : " + getValue("name", element2)+"\n");
tvParser.setText(tvParser.getText()+"Salary : " + getValue("salary", element2)+"\n");
}
}//end of for loop
} 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();
}
}
..................................................................................................................................................................
In this blog parse the xml used by DOM parser. The DOM parser better then SAX parser because DOM Parser Create And Parse xml file.
parser_file.xml.
Create the file inside assets folder.
...................................................................................................................................................................
<?xml version="1.0"?>
<records>
<employee>
<name>Sarvesh Kaushik</name>
<salary>50000</salary>
</employee>
<employee>
<name>Varun Chauhan</name>
<salary>60000</salary>
</employee>
<employee>
<name>Shashank Singh</name>
<salary>70000</salary>
</employee>
</records>
..................................................................................................................................................................
main_activity.xml.
Create the file inside res/layout folder.
...................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
android:layout_marginBottom="60dp" />
<TextView
android:id="@+id/textView_parser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Details"
android:gravity="center" />
</LinearLayout>
..................................................................................................................................................................
MainActivity.java.
...................................................................................................................................................................
package com.example.androidtestcode;
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 MainActivity extends Activity {
TextView tvParser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvParser = (TextView) findViewById(R.id.textView_parser);
try {
InputStream is = getAssets().open("pasrser_file.xml");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document documentfile = docBuilder.parse(is);
Element elements=documentfile.getDocumentElement();
elements.normalize();
NodeList nodList = documentfile.getElementsByTagName("employee");
for (int i=0; i<nodList.getLength(); i++) {
Node node = nodList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element2 = (Element) node;
tvParser.setText(tvParser.getText()+"\nName : " + getValue("name", element2)+"\n");
tvParser.setText(tvParser.getText()+"Salary : " + getValue("salary", element2)+"\n");
}
}//end of for loop
} 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();
}
}
0 comments