Java Reference
In-Depth Information
</CENTER></FONT>
Caution
XML documents must always be well formed. Most browsers can be used to check
that HTML documents are well formed, as can XML tools such as XML-Spy.
From a programming viewpoint, XML can be handled either as a character stream, or as an object. In a
stream based parser, XML elements are identified sequentially and used to trigger an event driven
processor. When the Document Object Model is used, the entire document is parsed into a document
object, in which the various elements and attributes can be accessed by name and path in much the
same way as files are accessed in a directory tree. The next section discusses the Document Object
Model.
XML and the Document Object Model
The Document Object Model (DOM) represents an XML document as a tree. The document element is
the top level of the tree. The document element has a number of child nodes that represent the
branches of the tree. Figure 17-1 shows an XML document displayed as a tree.
Figure 17-1: XML document displayed as a tree
The most basic component of the DOM is the Node interface. Every constituent component in a DOM
representation of an XML document is a node. The Node interface is extended by other interfaces such
as the Element interface and the Document interface. In the example shown in Figure 17-1 , the
document element is the CUSTOMERS element, highlighted in the JTree representation. You can see
in the text representation on the right-hand side that the CUSTOMERS element contains the attribute
node DBNAME= "CONTACTS".
CUSTOMERS is an element node, as are CUSTOMER, FIRST_NAME, and so on. The JTree
represents elements as folders. Within the element nodes are additional element nodes, as well as text
nodes, represented by a document icon, with the actual text printed next to the icon.
Representing the XML document as a tree structure of Java objects allows the Java programmer to
create, access, and modify XML documents and their contents through one of a number of widely
available APIs. Before discussing the DOM, it is appropriate to review the structure of an XML
document.
The XML Header
An XML file must always start with a declaration that identifies the document as an XML. The minimal
header looks like this:
<?xml version="1.0"?>
Search WWH ::




Custom Search