Basics of programming using DOM-Java XML

Started by thiruvasagamani, Aug 20, 2008, 11:25 AM

Previous topic - Next topic

thiruvasagamani

Basics of programming using DOM

After you have made sure that your environment has been set up correctly (see Setting up the environment for XML and Java section), you may write your first Java and XML example.
For this example we will use the DOM API discussed in the previous section.

This is a simple example that will read the text "Hello World" from a xml file called "hello.xml".


1) Import package org.w3c.dom

The Java interfaces have been defined by W3C and are contained in the package org.w3c.dom.


import org.w3c.dom.*;


2) Import Vendor dependent Parser.


The next step is to import a vendor dependent XML parser.
In our case it will be the xerces DOM parser that we configured.

import org.apache.xerces.parsers.DOMParser;


3) On calling DOMHelloWorld

The main method of DOMHelloWorld.java will check that the filename
of the xml file has been provided as an argument.


public static void main(String[] args)
{
if (args.length != 1)
{
System.out.println("usage: java DOMHelloWorld hello.xml");
System.exit(0);
}

String xmlfilename = args[0];

}


4) Parsing the XML document

First we must create an instance of the parser (vendor specific parser).
This is the same parser we imported earlier in step 2.


DOMParser xmlparser = new DOMParser();


5) Parse the xml file.

This is really easy because the parser does it for you.
All you have to do is call the parse method with the name of the xml file.


xmlparser.parse(xmlfilename);


If you look at the API documentation that comes with the Xerces parser
and search for the parse method you will notice something special.
DOMParser and SAXParser are subclasses of XMLParser.
The parse method throws two exceptions, SAXException and
java.io.IOException.

If you try to compile the source code without catching the exception, an error will occur (java.io.IOException must be caught).


Under the previous import statements add the imports
for the IOException and SAXException classes .


import java.io.IOException;
import org.xml.sax.SAXException;

So now we put a try/catch block around the parse method of DOMParser.

try
{
xmlparser.parse(xmlfilename);
}
catch (IOException e)
{
System.out.println("Error reading xml file: " e.getMessage());
}
catch (SAXException e)
{
System.out.println("Error in parsing: " e.getMessage());
}


6) Accessing the DOM tree

As DOM creates a tree-based structure based on the xml file,
we will need to access the information stored in the tree.
To access the tree call getDocument() and this returns a Document.


Document doc = xmlparser.getDocument();


This Document represents the entire XML document. The data we need to access is stored in nodes. These nodes can have child nodes. Therefore what you need to do next is walk through the Tree structure (Document) and display the data stored in the Nodes.


Now the Fun starts!!!


7) Walking the nodes

Next we will write a method to display the data in a node. The method will be called displayNode and will take one parameter, the start node.

This method will start from the first node and walk through all the nodes in the XML using recursion.

The nodes in the XML document are of different node types. The node types can be divided into two broad categories; structural nodes and content nodes.
Structural nodes are not actually part of the content in the document but are used to provide syntax structure.
Thiruvasakamani Karnan