Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

XML Parsing DOM


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

Let’s write the code to parse the xml using dom parser.

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

Output:

domxmlparsing

Comments

comments