Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
This property is read-only. The childNodes property returns
the IXMLDOMNodeList collection object of the child Nodes for the
parent object. Each Node that is returned from the child Node,
may have childNodes themselves, which is consistent with the tree
metaphor that the DOMDocument uses.
<ADDRESS>121 Zootle Road, Cape Town, South
Africa</ADDRESS>
<TEL>(++2721) 531 9090</TEL>
<FAX>(++2721) 531 9090</FAX>
<EMAIL>Tracey Wilson@somewhere.com</EMAIL>
</PERSON>
</PEOPLE>
Common uses of the
childNode property are:
¬ the child Node of the DOMDocument root consists of the
processing instructions, DTDs, etc.
¬ the child Node of the root element consists of all the
data in the DOMDocument
¬ the child Node of the attributes property of an
element
¬ consists of the collection of attributes for the given
element
What's so good
about the childNodes property?
¬ Because the childNodes property is an XMLDOMNodeList collection, it is easy
to iterate through, to collect the data.
¬ You can use the XMLDOMNode object to iterate through the
XMLDOMNodeList
collection, as it receives most of the other type of interfaces.
See later in this section, "Iterating throughthe
childNodes."
Getting the
childNodes from the root of the DOMDocument
Here we look for the child Nodes of the actual root of the
complete DOMDocument. To get these child Nodes from VB, our code
would read:
set objXMLDOMNodeList = objDOMDocument.childNodes
¬ This will return the four child Nodes shown in figure
.
¬ You might have noticed that we have specified the
Nodetypes returned from each child Node. Please see the nodeType
property for more details on these types of Nodes.
Getting the
childNodes from the documentElement element of the
DOMDocument
This property returns all the Nodes associated with root Node,
which is found in the documentElement property of the DOMDocument
(i.e., all the PERSON's in the PEOPLE element).To get the
childNodes from VB, our code would read:
set objXMLDOMNodeList =
objDOMDocument.document-Element.childNodes
From our example, two child Nodes will be returned in this
NodeList object, as shown in figure .
Getting all the
childNodes from a single element
We now want to return all the elements between each
section:
<PERSON PERSONID="p2">
...
</PERSON
This returns the NAME, ADDRESS, TEL, etc. This is the base of
the child Nodes, but even these childNodes can once again iterate
down to more childNodes, if they themselves have children.
In the following XML example, the child Node of Mark Wilson is
Male:
<PERSON PERSONID="p2">
<NAME>Mark Wilson</NAME>
<GENDER>Male</GENDER>
<ADDRESS>
...
</PERSON
In our main example in table , there are five child Nodes
found for each Person element, as shown in figure .
Iterating through
the childNodes
Because an IXMLDOMNodeList collection is returned from the
childNodes property, you can iterate through each item using a
for each ... next loop. Here we demonstrate looping through the
collection of child Nodes returned from the documentElement
property:
For Each objNode In
objDOMDocument.document-Element.child-Nodes
populateTreeWithChildren objNodeNext
The objNode has been declared as an IXMLDOMElement.
¬ Note that an XMLDOMNodeList object is returned even if
there are no children of the Node. In such a case, the length of
the list will be set to zero. To get the length of the
XMLDOMNodeList, use:
if objPeopleRoot.length > 0 then
...
end if
This manuscript is an abridged version of a chapter from the
Manning
Publications book XMLProgramming with VB and ASP. This chapter
looks at the Microsoft DOM objects. NOTE: Most images have been
removed to increase speed and many of the code comments have also
been removed for presentation. Please purchase the book to enjoy
the full experience of all the chapters with images and code
comments!