Wednesday 31 October 2012

How to parse xml file in Assert folder in Android using Dom Parser

This below example parse xml file which in assert folder of our project.
This file contains employee details this example parse xml file data and set to textview.

Data file (employeesdetails.xml) create this file assert folder .
In this example we read this file

<?xml version="1.0"?>
<employee_list>
<employee>
    <employee_id>1001</employee_id>
<employee_name>Android </employee_name>
<employee_salary>50000</employee_salary>
</employee>
<employee>
    <employee_id>1002</employee_id>
<employee_name>Surya </employee_name>
<employee_salary>60000</employee_salary>
</employee>
<employee>
    <employee_id>1003</employee_id>
<employee_name>Narayana </employee_name>
<employee_salary>40000</employee_salary>
</employee>
</employee_list>

Android UI Layout (activity_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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="86dp"
        android:layout_marginTop="77dp"
        android:text="TextView" />

</RelativeLayout>

Android Activity (MainActivity.java)

package com.androidsurya.xmlparsing;

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 textView;
InputStream inputStream;
DocumentBuilderFactory dbFactory;
DocumentBuilder docBuilder;
Document doc;
Element element;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView1);
try {
inputStream = getAssets().open("employeesdetails.xml");

dbFactory = DocumentBuilderFactory.newInstance();
docBuilder = dbFactory.newDocumentBuilder();
doc = docBuilder.parse(inputStream);

element = doc.getDocumentElement();
element.normalize();

NodeList nList = doc.getElementsByTagName("employee");
for (int i = 0; i < nList.getLength(); i++) {

Node node = nList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element2 = (Element) node;
textView.setText(textView.getText() + "\nEmployee Id : "
+ getValuefromTagName("employee_id", element2) + "\n");
textView.setText(textView.getText() + "Employee Name : "
+ getValuefromTagName("employee_name", element2) + "\n");
textView.setText(textView.getText() + "Employee Salary: "
+ getValuefromTagName("employee_salary", element2) + "\n");
textView.setText(textView.getText()
+ "===========================");
}
}
} catch (Exception e) {
e.printStackTrace();
}

}

private static String getValuefromTagName(String tag, Element element) {
NodeList nodeList = element.getElementsByTagName(tag).item(0)
.getChildNodes();
Node node = (Node) nodeList.item(0);
return node.getNodeValue();
}

}

Register Activity in Android Manifest file

 <activity
            android:name="com.androidsurya.xmlparsing.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Output Screenshot





















No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...