Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

XML Parsing SAX


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.     <TextView
  7.         android:id=“@+id/textView1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentLeft=“true”
  11.         android:layout_alignParentTop=“true”
  12.         android:layout_marginLeft=“75dp”
  13.         android:layout_marginTop=“46dp”
  14.         android:text=“TextView” />
  15. </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.Kreationnext.saxxmlparsing;
  2. import java.io.InputStream;
  3. import javax.xml.parsers.SAXParser;
  4. import javax.xml.parsers.SAXParserFactory;
  5. import org.xml.sax.Attributes;
  6. import org.xml.sax.SAXException;
  7. import org.xml.sax.helpers.DefaultHandler;
  8. import android.app.Activity;
  9. import android.os.Bundle;
  10. import android.widget.TextView;
  11. public class MainActivity extends Activity {
  12. TextView tv;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. tv=(TextView)findViewById(R.id.textView1);
  18. try {
  19. SAXParserFactory factory = SAXParserFactory.newInstance();
  20. SAXParser saxParser = factory.newSAXParser();
  21. DefaultHandler handler = new DefaultHandler() {
  22. boolean name = false;
  23. boolean salary = false;
  24. public void startElement(String uri, String localName,String qName,
  25. Attributes attributes) throws SAXException {
  26. if (qName.equalsIgnoreCase(“name”))
  27. {
  28. name = true;
  29. }
  30. if (qName.equalsIgnoreCase(“salary”))
  31. {
  32. salary = true;
  33. }
  34. }//end of startElement method
  35. public void endElement(String uri, String localName,
  36. String qName) throws SAXException {
  37. }
  38. public void characters(char ch[], int start, int length) throws SAXException {
  39. if (name) {
  40. tv.setText(tv.getText()+“\n\n Name : “ + new String(ch, start, length));
  41. name = false;
  42. }
  43. if (salary) {
  44. tv.setText(tv.getText()+“\n Salary : “ + new String(ch, start, length));
  45. salary = false;
  46. }
  47. }//end of characters
  48.  method
  49. };//end of DefaultHandler object
  50. InputStream is = getAssets().open(“file.xml”);
  51. saxParser.parse(is, handler);
  52. catch (Exception e) {e.printStackTrace();}
  53. }
  54. }

Output:

saxxmlparsing

Comments

comments