Accessing XML Using the DOM
DOM is based on an object structure that closely resembles the structure of the documents it models. For instance, consider the following XML document:<booklist>
<book isbn="0-07-213495-X">
<title>Oracle9i XML Handbook</title>
<author>Chang, Scardina and Kiritzov</author>
<publisher>Osborne</publisher>
<price>49.99</price>
</book>
</booklist>
In DOM, documents have a logical structure that is similar to a tree, also known as a structure model. In the example document, you can see the root element booklist serves as the root of the DOM tree, as you would expect. The root element contains one child, book, which has four children: title, author, publisher, and price; and one attribute, ISBN. The leaf nodes of the tree are simple text string values. The nodes in the DOM tree can be reached by using tree-walking methods (this does not include attributes). One important property of DOM structure models is structural isomorphism: if any two DOM implementations are used to create a representation of the same document, they create the same structure model. This means implementations are free to choose any data structure (not necessarily a tree) to implement DOM. When the XML parser parses the XML document, such a representation can be formed in memory.W3C has created a set of DOM APIs for accessing and navigating this structure. Again, the components of this structure are the root element of the document; elements; attributes; text nodes that represent the textual content of an element or attribute; CDATA sections to mark off blocks of text that would otherwise be regarded as markup; comments; entity references; processing instructions; and so forth. XML parsers that provide all the DOM APIs are considered to be compliant with the W3C DOM recommendation.
The following Java code sample demonstrates a simple use of the parser and DOM APIs. This sample demonstrates how to set parser options, parse the XML file given to the application, and print the element nodes and attribute values in the document.
import java.io.*;
import java.net.*;
import org.w3c.dom.*;
import org.w3c.dom.Node;
import oracle.xml.parser.v2.*;
public class DOMSample {
static public void main(String[] argv){
try {
if (argv.length != 1){
// Must pass in the name of the XML file.
System.err.println("Usage: java DOMSample filename");
System.exit(1);
}
// Get an instance of the parser
DOMParser parser = new DOMParser();
// Generate a URL from the filename.
URL url = createURL(argv[0]);
// Set various parser options: validation on,
// warnings shown, error stream set to stderr.
parser.setErrorStream(System.err);
parser.showWarnings(true);
// Parse the document.
parser.parse(url);
// Obtain the document.
Document doc = parser.getDocument();
// Print document elements
System.out.print("The elements are: ");
printElements(doc);
// Print document element attributes
System.out.println("The attributes of each element are: ");
printElementAttributes(doc);
}
catch (Exception e){
System.out.println(e.toString());
}
}
static void printElements(Document doc) {
NodeList nl = doc.getElementsByTagName("*");
Node n;
for (int i=0; i<nl.getLength(); i++){
n = nl.item(i);
System.out.print(n.getNodeName() + " ");
}
System.out.println();
}
static void printElementAttributes(Document doc){
NodeList nl = doc.getElementsByTagName("*");
Element e;
Node n;
NamedNodeMap nnm;
String attrname;
String attrval;
int i, len;
len = nl.getLength();
for (int j=0; j < len; j++){
e = (Element)nl.item(j);
System.out.println(e.getTagName() + ":");
nnm = e.getAttributes();
if (nnm != null){
for (i=0; i<nnm.getLength(); i++){
n = nnm.item(i);
attrname = n.getNodeName();
attrval = n.getNodeValue();
System.out.print(" " + attrname + " = " + attrval);
}
}
System.out.println();
}
}
}