Using DOM to Traverse XML

Introduction

XML is now used frequently to model business data in large-scale
applications. A common Java application task is to parse XML to
retrieve its data. The Document Object Model (DOM) defines a set of
interfaces for navigating and manipulating the content and structure
of XML and HTML documents.

Objective

After reading this article you will be able to create a
representation of an XML document in your Java program and traverse
that representation in two different ways. You will be able to
traverse a horizontal representation of the XML document, and you will
be able to traverse a tree, or hierarchical, representation of the XML
document.

The Document Object Model (DOM) in Detail

The DOM defines interfaces that allow programmers to navigate XML
and HTML documents and also to manipulate their content and
structure. The DOM is a specification of interfaces; it's not an
implementation. Vendors are left to come up with their own
implementation of DOM. Sun Microsystems has some DOM support in its
Java XML Processing API. Other vendors that provide support are IBM,
Oracle, and the Apache Software Foundation.

The DOM has several levels. Level 0 was the first requirements
document that defined functionality similar to that used in Netscape
Navigator 3.0 and Internet Explorer 3.0. On October 1, 1998, DOM Level
1 Recommendation was released which provided functionality to navigate
and manipulate the structure and content of XML and HTML
documents. DOM Level 2 is a set of specifications that add to the
functionality defined in DOM Level 1. The following list describes the
different recommendations of DOM Level 2.

DOM Level 2 Core Recommendation -- Further defines ways to
navigate and manipulate the content and structure of XML and HTML documents.
This recommendation is built on the DOM Level 1 Recommendation, filling in
some of the gaps.

DOM Level 2 Views Recommendation specifies an interface to
provide programmers the functionality to view alternate presentations
of the XML or HTML document. The interfaces defined in this
recommendation are optional, but if a vendor chooses to implement
them, they must also implement the DOM Level 2 Core
Recommendation.

DOM Level 2 Style Recommendation specifies an interface to
provide programmers the ability to dynamically access and manipulate
style sheets. The interfaces defined in this recommendation are
optional, but if a vendor chooses to implement the interfaces, they
must also implement the DOM Level 2 Core Recommendation.

DOM Level 2 Events Recommendation specifies a set of
interfaces to provide a generic event system to programmers. The
interfaces defined in this recommendation are optional, but if a
vendor chooses to implement them, they must also provide support for
the DOM Level 2 Core Recommendation.

DOM Level 2 Traversal-Range Recommendation specifies a set of
interfaces that allow programmers to traverse a representation of the
XML document. There is also a set of interfaces defined to manipulate
ranges of the XML document. The interfaces defined in this
recommendation are optional, but if a vendor chooses to implement
them, they must also implement the DOM Level 2 Core
Recommendation.

DOM Level 2 HTML Working Draft provides a set of interfaces
that allow programmers work on HTML documents. The interfaces defined
in this recommendation are optional, but if a vendor chooses to
implement them, they must also provide implementation for the DOM
Level 2 Core Recommendation.

Getting Started

This article explores the traversal of DOM representations of XML
documents from within Java applications. The Apache Software
Foundation has implemented the optional interfaces of the DOM Level 2
Traversal-Range Recommendation in their Xerces project. You can
download the
Xerces JAR, which contains the files you'll need. Xerces also
supports the optional interfaces defined in DOM Level 2 Events
Recommendation. Make sure to place the xerces.jar file in
your system CLASSPATH so the Java compiler will be able
to locate the appropriate files.

You also need a JDK to compile and run your Java programs. You can
download a JDK from Sun
Microsystems.

An Example XML Document

To learn to parse and traverse XML you'll need an example XML
document. Listing 1 is the DTD for the example XML document. It's a
simple representation of a bank. In this example a bank has clients,
employees, and a branch identification number.