AdSense

AdSense3

Wednesday, 7 October 2015

Android XML Parsing using DOM Parser

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.

Example of android DOM 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

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

Output:

DOM Xml Parsing

No comments:

Post a Comment