Parse the XML using DOM Parser in Java

December 8, 2011 | Programming

Java XML Parser

From Wikipedia, we can read like:

The DOM interface is perhaps the easiest to understand. It parses an entire XML document and constructs a complete in-memory representation of the document using the classes modeling the concepts found in the Document Object Model(DOM) Level 2 Core Specification

The following is the Java code to parse an XML document using DocumentBuilder:

Use the following XML as an input to the DOMParser class:

<?xml version="1.0" encoding="UTF-8"?>
  <Employee type="permanent">
  <Employee type="contract">
  <Employee type="permanent">

The following is the Java class which parses the above XML:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class DOMParser {
	public static void main(String[] args) {
		  // Factory API to obtain a parser that produces DOM object trees from XML documents.
		  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		  // To obtain DOM Document instances from an XML document
		  DocumentBuilder builder = factory.newDocumentBuilder();
		  // Get a new DOM Document object. 
		  Document document = builder.parse("C:\\new.xml");
		  // Get root element of the document
		  Node node = document.getDocumentElement();
		  String root = node.getNodeName();
		  System.out.println("Root Node name: " + root);			  
		  String tagName = "Employee";
		  // Get the list of elements by tag name "Employee"
		  NodeList nodes = document.getElementsByTagName(tagName);
		  System.out.println("Document contains " + nodes.getLength() + tagName +" elements.");
		  // Get all elements in the document
		  NodeList list = document.getElementsByTagName("*");
		  System.out.println("XML Elements: ");
		  for (int i = 0; i < list.getLength(); i++) {
			  // Get element
			  Element element = (Element)list.item(i);
			  String attrValue = element.getAttribute("type");
			  // If no attribute is not there the empty string will return
			  if (!attrValue.equals("")) {
				System.out.println("Attribute 'type' value: " + attrValue);
	  } catch (Exception e) {

Tags: , ,

Leave a Reply

If you have a Tip, Comment, Article or anything you want to share, please send it to: