Create and read XML files using DOM Parser

Create and read XML files using DOM Parser

In Java JDK there are two types of XML parsers models : DOM (Document Object Model) and SAX(Simple API for XML). In this series we are going to take a look at DOM parser and how to use Java DOM API to create & parse the XML files.

Where do we use the XML?
XML is a standard mechanism and universal syntax which can be used for data exchanges. The XML emphasize on simplicity, generality and usability over the web and portability across multiple environments and platforms. Lot of today’s application and frameworks uses the XML as this specifies a standard format which is readable across multiple systems and also human readable. E.g. Web services, RSS, XHTML, XML Encryption, XML database.

Let’s take an example of a simple XML document.

<?xml version="1.0" encoding="UTF-8"?>
  <product id="1">

The above example contains various XML elements including root element and text contents inside the tags. A typical structure of an XML document is shown in the below diagram.

XML Document Structure
XML Document Structure

Now let’s see how the XML processing happens. It is typically done through three steps:
a) Input processing
– Parse and validation of the source document
– Locate relevant information in the source document
– Fetch the required information
b) Process information
– Process the required information received from the XML source document
c) Output processing
– Generate the output document structure with the information

 Document Object Model (DOM) Parsing
DOM represents an XML Document into tree structure format in which each element represents tree branches. DOM parser creates the DOM Memory tree representation of XML file and then parses it, so it requires more memory. So it will be wise decision if we have to select the DOM parser only when we have a small XML file and if the DOM parser tries to parse a large XML source document then the processing time is very long and may run short of required memory. The DOM parser allows the users to access any part of the source document repeatedly.

DOM Parser
DOM Parser

Now let’s take a look at the sample code for XML parsing.

To parse the XML file we will need to create the DOM parser by using the DOM API.

Create a new instance of the DocumentBuilderFactory which enables applications to obtain a parser object that produces DOM object trees from XML document.

DocumentBuilderFactory docBldrFactry = DocumentBuilderFactory.newInstance();

Get the DOM parser (DocumentBuilder instance) from the factory class by calling docBldrFactry.newDocumentBuilder(). The document builder object is the actual DOM parser which can parse any XML file from various input sources including InputStreams, Files, URLs etc to produce a Document object.

DocumentBuilder	docBldrObj = docBldrFactry.newDocumentBuilder();

The Document interface represents the entire XML file. Conceptually, it is the root of the document tree, and provides
the primary access to the document’s data.

Document document = docBuilder.parse(file);
Element rootElement = document.getDocumentElement();
DOM Parser parsing
DOM Parser parsing

The elements, text nodes, comments are all part and parcel of the Document. We can derive all the child nodes from the root elements as a part of a NodeList and iterate through the NodeList to derive the values for each nodes.

NodeList ndList = rootElement.getChildNodes();
for (int i = 0; i < ndList.getLength(); i++) {
				Node eNode = ndList.item(i);
				if (eNode instanceof Element) {
					Element child = (Element) eNode;
					System.out.println("\n nodelist length"
							+ ndList.getLength());
					System.out.println("Node Name: " + eNode.getNodeName());

					System.out.println("Product Name: "
							+ getNodeValue("ProductName", child));
					System.out.println("SKU: " + getNodeValue("sku", child));

In the next series we will discuss about the SAX parser and various differences between DOM and SAX models.

<<Sample Code DOM Parser>>


Share This Post

One Response to "Create and read XML files using DOM Parser"

Post Comment