XML Objectified: IDOMElement's Revisited
December 21, 1998
Despite our brief sojourn into the
attributes
list and its IDOMAttribute objects, we have not yet abandoned
the
IDOMElement object,
since it is the object which
reflects the XML element. To recap our latest feat, we've
begun at the trunk (petfolio node), worked
our way up to see each branch (pet node)
and briefly peeked at the attributes associated with the
latter node.
If you look closely at each pet branch in
the earlier inspired
illustration
you'll see that each possesses five sub-branches. The
leaves, in fact, are connected to each of these sub-branches.
Similarly, the <pet> element in our
example contains five child elements: <breed>,
<color>, <age>,
<weight>, and
<description>. Each of these
elements, then, is considered by the XML DOM to be another
element node, meaning yet another IDOMNode object:
|
petbase.childNodes(1).childNodes(0).childNodes.length |
yields
|
5 |
|
petbase.childNodes(1).childNodes(0).childNodes(0).nodeName |
yields |
"breed" |
|
petbase.childNodes(1).childNodes(0).childNodes(1).nodeName |
yields |
"color" |
|
petbase.childNodes(1).childNodes(0).childNodes(2).nodeName |
yields |
"age" |
|
petbase.childNodes(1).childNodes(0).childNodes(3).nodeName |
yields |
"weight" |
|
petbase.childNodes(1).childNodes(0).childNodes(4).nodeName |
yields |
"description" |
Just to flex our syntactical muscles, then, let's
take a quick peek at the units attribute of the weight
node:
|
petbase.childNodes(1).childNodes(0).childNodes(3).attributes.getNamedItem("units") |
yields
|
"lbs" |
Whew -- if you need a Tylenol at this point that would
be entirely understandable. We're way out on a limb now!
Our final goal in this lesson, then, is to reach out for
that brass monkey -- the data within, the
IDOMText object. The leaf nodes are
within sight. In fact, each of the above nodes possesses
only one child, the data itself:
the leaf node.
|
petbase.childNodes(1).childNodes(0).childNodes(3).childNodes.length |
yields
|
1 |
We know that the child node at this point is in
fact a leaf node due to its nodeType, #3 NODE_TEXT, which
cannot possess any children. Thus, it is an IDOMText object.
|
petbase.childNodes(1).childNodes(0).childNodes(3).childNodes(0).nodeType |
yields
|
3 |
The end is near ... the nodeValue property will
contain the data itself, the text content of the leaf
which is a nodeType #3. Voila:
|
petbase.childNodes(1).childNodes(0).childNodes.length |
yields
|
5 |
|
petbase.childNodes(1).childNodes(0).childNodes(0).firstChild.nodeValue
|
yields |
"Domestic Shorthair" |
|
petbase.childNodes(1).childNodes(0).childNodes(1).firstChild.nodeValue
|
yields |
"black/tan tiger" |
|
petbase.childNodes(1).childNodes(0).childNodes(2).firstChild.nodeValue
|
yields |
"3" |
|
petbase.childNodes(1).childNodes(0).childNodes(3).firstChild.nodeValue
|
yields |
"1.75" |
|
petbase.childNodes(1).childNodes(0).childNodes(4).firstChild.nodeValue
|
yields |
"Ella is a smart, poised creature full of courage
and vinegar." |
Exciting stuff -- in the interest of prudence, we should
note that the nodeValue property returns a string
value, even if the data is numeric. In the above the leaf
nodes with the data "3" and "1.75",
these values are returned as strings. The length
property, in contrast, is an integer.
XML Objectified: Attributes Considered
XML via the Document Object Model: A Preliminary Course
A Final Image...
|