Android XML Parsing using SAX Parser And DOM 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.

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="50dp"  
  14.         android:layout_marginTop="50dp"  
  15.         android:text="TextView" />  
  16.   
  17. </RelativeLayout>  

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. }  

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>Windows</name>  
  5. <salary>50000</salary>  
  6. </employee>  
  7. <employee>  
  8. <name>Ios</name>  
  9. <salary>60000</salary>  
  10. </employee>  
  11. <employee>  
  12. <name>Android</name>  
  13. <salary>70000</salary>  
  14. </employee>  
  15. </records>  

DOM Xml parsing

We can parse the xml document by dom parser also. It can be used to create and parse the xml file.

Advantage of DOM Parser over SAX

It can be used to create and parse the xml file both but SAX parser can only be used to parse the xml file.

Disadvantage of DOM Parser over SAX

It consumes more memory than SAX.

Both XML and  file.xml are same for both Parsings.

 

Activity class

Let's write the code to parse the xml using dom parser.
File: MainActivity.java
  1. package com.javatpoint.domxmlparsing;  
  2. import java.io.InputStream;  
  3.   
  4. import javax.xml.parsers.DocumentBuilder;  
  5. import javax.xml.parsers.DocumentBuilderFactory;  
  6. import org.w3c.dom.Document;  
  7. import org.w3c.dom.Element;  
  8. import org.w3c.dom.Node;  
  9. import org.w3c.dom.NodeList;  
  10. import android.app.Activity;  
  11. import android.os.Bundle;  
  12. import android.widget.TextView;  
  13.   
  14. public class MainActivity extends Activity {  
  15. TextView tv1;  
  16.   
  17. @Override  
  18. public void onCreate(Bundle savedInstanceState) {  
  19. super.onCreate(savedInstanceState);  
  20. setContentView(R.layout.activity_main);  
  21. tv1=(TextView)findViewById(R.id.textView1);  
  22. try {  
  23. InputStream is = getAssets().open("file.xml");  
  24.   
  25. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();  
  26. DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();  
  27. Document doc = dBuilder.parse(is);  
  28.   
  29. Element element=doc.getDocumentElement();  
  30. element.normalize();  
  31.   
  32. NodeList nList = doc.getElementsByTagName("employee");  
  33. for (int i=0; i<nList.getLength(); i++) {  
  34.   
  35. Node node = nList.item(i);  
  36. if (node.getNodeType() == Node.ELEMENT_NODE) {  
  37. Element element2 = (Element) node;  
  38. tv1.setText(tv1.getText()+"\nName : " + getValue("name", element2)+"\n");  
  39. tv1.setText(tv1.getText()+"Salary : " + getValue("salary", element2)+"\n");  
  40. tv1.setText(tv1.getText()+"-----------------------");  
  41. }  
  42. }//end of for loop  
  43.   
  44. catch (Exception e) {e.printStackTrace();}  
  45.   
  46. }  
  47. private static String getValue(String tag, Element element) {  
  48. NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();  
  49. Node node = (Node) nodeList.item(0);  
  50. return node.getNodeValue();  
  51. }  
  52.   
  53. }  

No comments:

Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...