Android XML Parsing using SAX Parser


Android XML parsing using SAX Parser

Android provides the facility to parse the xml file using SAX, DOM etc. parsers. The SAX parser cannot be used to create the XML file, It can be used to parse the xml file only.

Advantage of SAX Parser over DOM

Example of android SAX Xml parsing

activity_main.xml

File: activity_main.xml
<RelativeLayout xmlns:androclass="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="75dp"  
        android:layout_marginTop="46dp"  
        android:text="TextView" />  
  
</RelativeLayout>  


xml document

File: file.xml
<?xml version="1.0"?>  
<records>  
<employee>  
<name>Sachin Kumar</name>  
<salary>50000</salary>  
</employee>  
<employee>  
<name>Rahul Kumar</name>  
<salary>60000</salary>  
</employee>  
<employee>  
<name>John Mike</name>  
<salary>70000</salary>  
</employee>  
</records>  


Activity class

File: MainActivity.java
  1. package com.dheeruapps.saxxmlparsing;  
  2.   
  3.   
  4. import java.io.InputStream;  
  5. import javax.xml.parsers.SAXParser;  
  6. import javax.xml.parsers.SAXParserFactory;  
  7. import org.xml.sax.Attributes;  
  8. import org.xml.sax.SAXException;  
  9. import org.xml.sax.helpers.DefaultHandler;  
  10. import android.app.Activity;  
  11. import android.os.Bundle;  
  12. import android.widget.TextView;  
  13. public class MainActivity extends Activity {  
  14. TextView tv;  
  15. @Override  
  16.   
  17. public void onCreate(Bundle savedInstanceState) {  
  18. super.onCreate(savedInstanceState);  
  19. setContentView(R.layout.activity_main);  
  20. tv=(TextView)findViewById(R.id.textView1);  
  21. try {  
  22. SAXParserFactory factory = SAXParserFactory.newInstance();  
  23.   
  24. SAXParser saxParser = factory.newSAXParser();  
  25.   
  26.   
  27. DefaultHandler handler = new DefaultHandler() {  
  28.   
  29. boolean name = false;  
  30.   
  31. boolean salary = false;  
  32.   
  33.   
  34. public void startElement(String uri, String localName,String qName,  
  35. Attributes attributes) throws SAXException {  
  36. if (qName.equalsIgnoreCase("name"))  
  37. {  
  38. name = true;  
  39. }  
  40. if (qName.equalsIgnoreCase("salary"))  
  41. {  
  42. salary = true;  
  43. }  
  44. }//end of startElement method  
  45. public void endElement(String uri, String localName,  
  46. String qName) throws SAXException {  
  47. }  
  48.   
  49. public void characters(char ch[], int start, int length) throws SAXException {  
  50. if (name) {  
  51.   
  52. tv.setText(tv.getText()+"\n\n Name : " + new String(ch, start, length));  
  53. name = false;  
  54. }  
  55. if (salary) {  
  56. tv.setText(tv.getText()+"\n Salary : " + new String(ch, start, length));  
  57. salary = false;  
  58. }  
  59. }//end of characters  
  60.  method  
  61. };//end of DefaultHandler object  
  62.   
  63. InputStream is = getAssets().open("file.xml");  
  64. saxParser.parse(is, handler);  
  65.   
  66. catch (Exception e) {e.printStackTrace();}  
  67. }  
  68. }  
Output -: 


















0 comments:

Post a Comment

My Instagram