AdSense

AdSense3

Tuesday, 6 October 2015

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

It consumes less memory than DOM.

Example of android SAX Xml parsing

activity_main.xml

Drag the one textview from the pallete. Now the activity_main.xml file will look like this:
File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginLeft="75dp"  
  14.         android:layout_marginTop="46dp"  
  15.         android:text="TextView" />  
  16.   
  17. </RelativeLayout>  

xml document

Create an xml file named file.xml inside the assets directory of your project.
File: file.xml
  1. <?xml version="1.0"?>  
  2. <records>  
  3. <employee>  
  4. <name>Sachin Kumar</name>  
  5. <salary>50000</salary>  
  6. </employee>  
  7. <employee>  
  8. <name>Rahul Kumar</name>  
  9. <salary>60000</salary>  
  10. </employee>  
  11. <employee>  
  12. <name>John Mike</name>  
  13. <salary>70000</salary>  
  14. </employee>  
  15. </records>  

Activity class

Now write the code to parse the xml using sax parser.
File: MainActivity.java
  1. package com.javatpoint.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:

SAX Xml Parsing

No comments:

Post a Comment