Java DOM Parser - Parse XML Document


Steps to Using DOM

Following are the steps used while parsing a document using DOM Parser.

  • Import XML-related packages.

  • Create a DocumentBuilder

  • Create a Document from a file or stream

  • Extract the root element

  • Examine attributes

  • Examine sub-elements

Import XML-related packages

import org.w3c.dom.*;
import javax.xml.parsers.*;

Create a DocumentBuilder

DocumentBuilderFactory factory =
DocumentBuilder builder = factory.newDocumentBuilder();

Create a Document from a file or stream

StringBuilder xmlStringBuilder = new StringBuilder();
xmlStringBuilder.append("<?xml version="1.0"?> <class> </class>");
ByteArrayInputStream input =  new ByteArrayInputStream(
Document doc = builder.parse(input);

Extract the root element

Element root = document.getDocumentElement();

Examine attributes

//returns specific attribute
//returns a Map (table) of names/values

Examine sub-elements

//returns a list of subelements of specified name
//returns a list of all child nodes

Demo Example

Here is the input xml file we need to parse:

<?xml version="1.0"?>
   <student rollno="393">
   <student rollno="493">
   <student rollno="593">

Demo Example:

package com.tutorialspoint.xml;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

public class DomParserDemo {
   public static void main(String[] args){

      try {	
         File inputFile = new File("input.txt");
         DocumentBuilderFactory dbFactory 
            = DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
         Document doc = dBuilder.parse(inputFile);
         System.out.println("Root element :" 
            + doc.getDocumentElement().getNodeName());
         NodeList nList = doc.getElementsByTagName("student");
         for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            System.out.println("\nCurrent Element :" 
               + nNode.getNodeName());
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
               Element eElement = (Element) nNode;
               System.out.println("Student roll no : " 
                  + eElement.getAttribute("rollno"));
               System.out.println("First Name : " 
                  + eElement
               System.out.println("Last Name : " 
               + eElement
               System.out.println("Nick Name : " 
               + eElement
               System.out.println("Marks : " 
               + eElement
      } catch (Exception e) {

This would produce the following result:

Root element :class

Current Element :student
Student roll no : 393
First Name : dinkar
Last Name : kad
Nick Name : dinkar
Marks : 85

Current Element :student
Student roll no : 493
First Name : Vaneet
Last Name : Gupta
Nick Name : vinni
Marks : 95

Current Element :student
Student roll no : 593
First Name : jasvir
Last Name : singn
Nick Name : jazz
Marks : 90