View RSS Feed

My Java Tips

Parsing XML documents using DOM

Rate this Entry
by , 11-09-2011 at 06:48 PM (1516 Views)
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 Code:
 <?xml version="1.0"?>
      <name id="MS1">Pollack</name>
      <name id="MS2">Neumann</name>
      <hobby>Reading books</hobby>
      <name id="MS3">Ken</name>
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.

Java Code:
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.

Java Code:
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 =
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
      line = (Element) hobby.item(j);
      System.out.println("Hobby: " + line.getFirstChild().getTextContent());
catch (Exception e) {



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

Submit "Parsing XML documents using DOM" to Facebook Submit "Parsing XML documents using DOM" to Digg Submit "Parsing XML documents using DOM" to Submit "Parsing XML documents using DOM" to StumbleUpon Submit "Parsing XML documents using DOM" to Google

Updated 11-09-2011 at 07:06 PM by Java Tip

Tags: None Add / Edit Tags