Parsing XML documents using DOM

19 November 2007

Parsing XML documents using DOM is easy and simple. Let do this with example.
DocumentBuilder is used to obtain Document from XML file. It is an abstract class. Once we have reference of DocumentBuilder, we will create Document instance. Document class instance is created using parse method of DocumentBuilder class which takes xml file as parameter. By doing so, we will have XML document in the memory.

We have following XML file to parse:

<?xml version="1.0"?>
<college>
  <student>
      <name id="MS1">Pollack</name>
      <age>27</age>
      <hobby>Hockey</hobby>
      <hobby>Music</hobby>
  </student>
  <student>
      <name id="MS2">Neumann</name>
      <age>26</age>
      <hobby>Football</hobby>
      <hobby>Reading books</hobby>
  </student>
  <student>
      <name id="MS3">Ken</name>
      <age>33</age>
      <hobby>Swimming</hobby>
  </student>
</college>

Now we want to get all the nodes for students in NodeList instance. We will use getElementsByTagName method of Document class and will provide the name of the tag as parameter. For our example, we mentioned student as parameter.

NodeList nodes = doc.getElementsByTagName("student");

Now we have all the student nodes in the instance called nodes. We will iterate through each node and will fetch and display the required data. Each student has name, age and hobbies. Hobbies can be more than one. Also there is an attribute called id related to name.

Element object is created that will point to one particular student node. A student element has child nodes (name, age, hobbies). We will create separate NodeList object for name, age and hobbies using getElementsByTagName method.

nodeListName.getLength() will return the no of nodes for a particular node. Rest is simple.

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
 
 
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
 
 
public class DOMParser {
 
public static void main(String[] args) {
 
 
 
File file = new File("C:\\howto.xml");
try {
DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(file);
 
NodeList nodes = doc.getElementsByTagName("student");
 
for (int i = 0; i < nodes.getLength(); i++) {
 
   Element element = (Element) nodes.item(i);
 
   NodeList name = element.getElementsByTagName("name");
   Element line = (Element) name.item(0);
 
   System.out.println("Student name: " + line.getFirstChild().getTextContent());
 
   System.out.println("Id: " + line.getAttribute("id"));
 
   NodeList age = element.getElementsByTagName("age");
   line = (Element) age.item(0);
   System.out.println("Age: " + line.getFirstChild().getTextContent());
 
   NodeList hobby = element.getElementsByTagName("hobby");
   for(int j=0;j<hobby.getLength();j++)
   {
      line = (Element) hobby.item(j);
      System.out.println("Hobby: " + line.getFirstChild().getTextContent());
   }
   System.out.println("-----------------------------------------------");
}
}
catch (Exception e) {
e.printStackTrace();
}
 
}
 
}

Output:

Student name: Pollack
Id: MS1
Age: 27
hobby: Hockey
hobby: Music
-----------------------------------------------
Student name: Neumann
Id: MS2
Age: 26
hobby: Football
hobby: Reading books
-----------------------------------------------
Student name: Ken
Id: MS3
Age: 33
hobby: Swimming
-----------------------------------------------

del.icio.us:Parsing XML documents using DOM  digg:Parsing XML documents using DOM  spurl:Parsing XML documents using DOM  wists:Parsing XML documents using DOM  simpy:Parsing XML documents using DOM  newsvine:Parsing XML documents using DOM  blinklist:Parsing XML documents using DOM  furl:Parsing XML documents using DOM  reddit:Parsing XML documents using DOM  fark:Parsing XML documents using DOM  blogmarks:Parsing XML documents using DOM  Y!:Parsing XML documents using DOM  smarking:Parsing XML documents using DOM  magnolia:Parsing XML documents using DOM  segnalo:Parsing XML documents using DOM  gifttagging:Parsing XML documents using DOM

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Parsing XML documents using DOM

One Response to “Parsing XML documents using DOM”

  1. YouNeedJava Says:

    Very big tnx.! Good article for start…

Leave a Reply