XML Objectified: IDOMDocument
December 21, 1998
The entire XML document is reflected through the
IDOMDocument
object -- an unwieldy name, but a name we use in principle
rather than practice.
Through this object we may access all others in
the XML tree. How, then, do we begin referencing this object?
We've created an HTML document which contains both our
script code and XML document. This allows for easy access
between the two. The HTML container document is quite simple:
<html>
<body>
<xml id="petbase" src="petfolio.xml"></xml>
<script language="JavaScript">
...code which accesses XML tree...
</script>
</body>
</html>
|
Notice that we've kept the XML document "petfolio.xml"
separate from this HTML document. The XML document
is assigned the identifier petbase. This,
then, is the name of the IDOMDocument object for this XML
tree -- petbase.
An XML document may contain only one root element. Looking
back at the petfolio.xml file, you will notice that
this root element is named petfolio. All
other elements in this file are contained within petfolio.
The petfolio element, then, represents the
root node, or object, of the XML tree, illustrated as
a "trunk" in the earlier graphic.
Petbase, our root document object, possesses
two nodes, one of which is the petfolio
node. Since the document may only contain a single root
node, why does petbase possess two? The other
node is not, in fact, an element, but rather a processing
instruction -- the line <?xml version="1.0"?>
at the top of the petfolio.xml file. Each of these nodes
is reflected via the DOM as an IDOMNode object, which we'll soon look
at more closely.
Visually, then, let's consider where we're at: we have a
root document which is an IDOMDocument object named
petbase. This object possesses two nodes;
1. a processing instruction and 2. a root element node
named petfolio. Yet another tree, at
a higher level of abstraction:

In total, then, petbase possesses two child
nodes -- defined in source order, the processing instruction
is the first node and the petfolio tree is the
second node. You can access these nodes via the childNodes
list, which is a zero-based array containing each IDOMNode
object who is a child of IDOMDocument. In other
words:
|
petbase.childNodes(0) |
references
|
the first node (the processing instruction) |
|
petbase.childNodes(1) |
references
|
the second node (petfolio) |
|
NB: Despite the fact that traditional JavaScript syntax
uses brackets to enclose array indices (e.g.
arrayName[1]
) you must use parentheses when referencing arrays
of XML objects. It is unknown at this time whether this is
a quirk in Microsoft's beta implementation of the XML
DOM or an intentional deviation. |
Alternatively, you could reference these nodes by using
the firstChild or lastChild properties
of the IDOMDocument object. In this case, since
petbase is a parent to only two child nodes:
|
petbase.firstChild |
references
|
the first node (the processing instruction) |
|
petbase.lastChild |
references
|
the second node (petfolio) |
Now, then, you know how to reference any of the child
nodes of the root document -- the IDOMDocument object.
"Exploring" the Tree
XML via the Document Object Model: A Preliminary Course
XML Objectified: IDOMNode's
|