Open top menu



Android XML Parsing using SAX Parser ?


In this blog we will explain how to parse xml file.Android SAX Parser provide the facility xml file parse .but parse the data on xml file.





parser_file.xml.
create a xml 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>
...................................................................................................................................................................
activity_main.
Create the view 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.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

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 {

SAXParserFactory pasrserFacrtory = SAXParserFactory.newInstance();
SAXParser sax_parser = pasrserFacrtory.newSAXParser();

DefaultHandler handler = new DefaultHandler() {
boolean name = false;
boolean salary = false;

public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
if (qName.equalsIgnoreCase("name")) {
name = true;
}
if (qName.equalsIgnoreCase("salary")) {
salary = true;
}
}

public void endElement(String uri, String localName,
String qName) throws SAXException {
}

public void characters(char ch[], int start, int length)
throws SAXException {
if (name) {
tvParser.setText(tvParser.getText() + "\n\n Name : "
+ new String(ch, start, length));
name = false;
}
if (salary) {
tvParser.setText(tvParser.getText() + "\n Salary : "
+ new String(ch, start, length));
salary = false;
}
}// close of characters

};// close of DefaultHandler

InputStream is = getAssets().open("pasrser_file.xml");
sax_parser.parse(is, handler);

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

}
}
...................................................................................................................................................................


Tagged

0 comments