|
Home TOC |
|
Examining the Structure of a DOM
In this section, you'll use the GUI-fied
DomEchoapp you created in the last section to visually examine a DOM. You'll see what nodes make up the DOM, and how they are arranged. With the understanding you acquire, you'll be well prepared to construct and modify Document Object Model structures in the future.Displaying A Simple Tree
We'll start out by displaying a simple file, so you get an idea of basic DOM structure. Then we'll look at the structure that results when you include some of the more advanced XML elements.
Note: The code used to create the figures in this section is inDomEcho02.java. The file displayed isslideSample01.xml. (The browsable version isslideSample01-xml.html.)
Figure 1 shows the tree you see when you run the DomEcho program on the first XML file you created in the DOM tutorial.
Figure 1 Figure 1: Document, Comment, and Element Nodes Displayed
Recall that the first bit of text displayed for each node is the element
type. After that comes the elementname, if any, and then the elementvalue. This view shows three element types:Document,Comment, andElement. There is onlyDocumenttype for the whole tree--that is the root node. TheCommentnode displays thevalueattribute, while theElementnode displays the elementname, "slideshow".Compare the Table 1: with the code in the AdapterNode's
toStringmethod to see whether the name or value is being displayed for a particular node. If you need to make it more clear, modify the program to indicate which property is being displayed (for example, with N: name, V: value).Expanding the slideshow element brings up the display shown in Figure 2.
Figure 2 Element Node Expanded, No Attribute Nodes Showing
Here, you can see the
Textnodes andCommentnodes that are interspersed between Slide elements. The emptyTextnodes exist because there is no DTD to tell the parser that no text exists. (Generally, the vast majority of nodes in a DOM tree will beElementandTextnodes.)Text nodes exist under element nodes in a DOM, and data is always stored in text nodes. Perhaps the most common error in DOM processing is to navigate to an element node and expect it to contain the data that is stored in the XML file. Not so! Even the simplest element node has a text node under it. For example, given
<size>12</size>, there is an element node (size), and a text node under it which contains the actual data (12).Notably absent from this picture are the
Attributenodes. An inspection of the table inorg.w3c.dom.Nodeshows that there is indeed an Attribute node type. But they are not included as children in the DOM hierarchy. They are instead obtained via the Node interfacegetAttributesmethod.
Note: The display of the text nodes is the reason for including the lines below in the AdapterNode'stoStringmethod. If your remove them, you'll see the funny characters (typically square blocks) that are generated by the newline characters that are in the text.
String t = domNode.getNodeValue().trim(); int x = t.indexOf("); if (x >= 0) t = t.substring(0, x); s += t;Displaying a More Complex Tree
Here, you'll display the example XML file you created at the end of the SAX tutorial, to see how entity references, processing instructions, and CDATA sections appear in the DOM.
Note: The file displayed in this section isslideSample10.xml. TheslideSample10.xmlfile referencesslideshow3.dtdwhich, in turn, referencescopyright.xmland a (very simplistic)xhtml.dtd.(The browsable versions areslideSample10-xml.html,slideshow3-dtd.html,copyright-xml.html, andxhtml-dtd.html.)
Figure 3 shows the result of running the
DomEchoapp onslideSample10.xml, which includes aDOCTYPEentry that identifies the document's DTD.Figure 3 DocType Node Displayed
The
DocTypeinterface is actually an extension ofw3c.org.dom.Node. It defines agetEntitiesmethod that you would use to obtainEntitynodes--the nodes that define entities like theproductentity, which has the value "WonderWidgets". LikeAttributenodes,Entitynodes do not appear as children of DOM nodes.When you expand the
slideshownode, you get the display shown in Figure 4.Figure 4 Processing Instruction Node Displayed
Here, the processing instruction node is highlighted, showing that those nodes do appear in the tree. The
nameproperty contains the target-specification, which identifies the app that the instruction is directed to. Thevalueproperty contains the text of the instruction.Note that empty text nodes are also shown here, even though the DTD specifies that a
slideshowcan containslideelements only, never text. Logically, then, you might think that these nodes would not appear. (When this file was run through the SAX parser, those elements generatedignorableWhitespaceevents, rather thancharacterevents.)The empty text elements are included because by default,
DocumentBuildercreates a DOM that includes all the lexical information necessary to reconstruct the original document, in it's original form. That includes comment nodes as well as text nodes. There is as yet no standard mechanism for eliminating such lexical information in the DOM so you are left with the logical structure.Moving down to the second
slideelement and opening theitemelement under it brings up the display shown in Figure 5.Figure 5 Entity Reference Node Displayed
Here, the Entity Reference node is highlighted. Note that the entity reference contains multiple nodes under it. This example shows only comment and a text nodes, but the entity could conceivable contain other element nodes, as well.
Moving down to the last
itemelement under the lastslidebrings up the display shown in Figure 6.Here, the
CDATAnode is highlighted. Note that there are no nodes under it. Since aCDATAsection is entirely uninterpreted, all of its contents are contained in the node'svalueproperty.Finishing Up
At this point, you have seen most of the nodes you will ever encounter in a DOM tree. There are one or two more that we'll mention in the next section, but you now know what you need to know to create or modify a DOM structure. In the next section, you'll see how to convert a DOM into a
JTreethat is suitable for an interactive GUI. Or, if you prefer, you can skip ahead to the 5th section of the DOM tutorial, Creating and Manipulating a DOM, where you'll learn how to create a DOM from scratch.
|
Home TOC |
|