Tuesday, June 23, 2009

Java code to parse XML using DOM (Document Object Model)

In the earlier article, we have gone through the SAX parsing concept. Here I am going to put source code of parsing an XML document using DOM.

There are two codes, one of which will be using JDK 1.5 for parsing the document and the second one is the code which will use xerces to parse the document (which we have used in SAX parsing).

Here we are going to use the same XML (Company.xml) which we have mentioned in the SAX parsing article.






/*Relevant APIs - http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/package-summary.html
http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/parsers/package-summary.html
*/
//Using Parser available with standard JDK 1.5
import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DOMParser
{
private Document doc = null;
public DOMParser()
{
try
{
doc = parserXML(new File("Company.xml"));
visit(doc, 0);
}
catch(Exception error)
{
error.printStackTrace();
}
}

//Recursively call the nodes and the elements and text nodes

public void visit(Node node, int level)
{
NodeList nl = node.getChildNodes();
for(int i=0, cnt=nl.getLength(); i < cnt; i++)
{
System.out.println("Nodes: "+nl.item(i));
visit(nl.item(i), level+1);
}
}

public Document parserXML(File file) throws SAXException, IOException, ParserConfigurationException
{
return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
}

public static void main(String[] args)
{
new DOMParser();
}
}










/*
To download the jar (xercesImpl.jar) -> http://www.apache.org/dist/xerces/j/
APIs - http://xerces.apache.org/xerces-j/apiDocs/org/apache/xerces/parsers/DOMParser.html
http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/package-summary.html
http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/parsers/package-summary.html
*/

//DOM parsing example using Xerces Parser

// Core Java APIs
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;

// Apache - Xerces Parser
import org.apache.xerces.parsers.DOMParser;

// DOM
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;

// SAX
import org.xml.sax.SAXException;

public class DOMParserXerces {

private static void printNode(Node node, String indent) {

switch (node.getNodeType()) {

case Node.DOCUMENT_NODE:
// recurse on each child
NodeList nodes = node.getChildNodes();
if (nodes != null) {
for (int i=0; i < nodes.getLength(); i++) {
printNode(nodes.item(i), "");
}
}
break;

case Node.ELEMENT_NODE:
String name = node.getNodeName();
System.out.println("Element Name = " + name);
NamedNodeMap attributes = node.getAttributes();
for (int i=0; i < attributes.getLength(); i++) {
Node current = attributes.item(i);
System.out.println(" " + current.getNodeName() +"=\"" + current.getNodeValue() + "\"");
}

// recurse on each child
NodeList children = node.getChildNodes();
if (children != null) {
for (int i=0; i < children.getLength(); i++) {
printNode(children.item(i), indent + " ");
}
}

break;

case Node.TEXT_NODE:
System.out.println("Text Node Value = " + node.getNodeValue());
break;
}

}


/**
* Sole entry point to the class and application.
* @param args Array of String arguments.
*/
public static void main(String[] args) {

// Create URL String from incoming file
String url = "file:" + "./Company.xml";

System.out.println();
System.out.println("Object to Parse (String URL) : " + url);
System.out.println();

try {
// Create a DOM Parser
DOMParser parser = new DOMParser();
// Parser the incoming file (URL)
parser.parse(url);
// Obtain the document
Document doc = parser.getDocument();
// Print the document from the DOM tree and feed it an initial
// indentation of nothing
printNode(doc,"");
System.out.println("\n");
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException saxe) {
saxe.printStackTrace();
}
}

}

No comments:

Total Pageviews