Parse the XML using DOM Parser in Java

December 8, 2011 | Programming

Java XML Parser

From Wikipedia http://en.wikipedia.org/wiki/Java_API_for_XML_Processing#DOM_interface, 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"?>
<Personnel>
  <Employee type="permanent">
        <Name>Seagull</Name>
        <Id>3674</Id>
        <Age>34</Age>
   </Employee>
  <Employee type="contract">
        <Name>Robin</Name>
        <Id>3675</Id>
        <Age>25</Age>
    </Employee>
  <Employee type="permanent">
        <Name>Crow</Name>
        <Id>3676</Id>
        <Age>28</Age>
    </Employee>
</Personnel>

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

import java.io.File;
 
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) {
 
		try{  
		  // 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);
			  System.out.println(element.getNodeName());
 
			  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) {
		  e.printStackTrace();
	  }
 
	}
}

Tags: , ,

Leave a Reply

If you have a Tip, Comment, Article or anything you want to share, please send it to: i@poisure.com