   
childNodes
Is a member of:
|
DOMDocument
XMLDOMNode
XMLDOMAttribute
XMLDOMCDATASection
XMLDOMComment
XMLDOMDocumentFragment
XMLDOMDocumentType
XMLDOMElement
XMLDOMEntity
XMLDOMEntityReference
XMLDOMNotation
XMLDOMProcessingInstruction
XMLDOMText
XTLRuntime
|
Syntaxset objXMLDOMNodeList = objXMLDOMNode.childNodes
RemarkThis 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.
ExampleDim objDOMDocument As DOMDocument
Dim objXMLDOMNodeList As IXMLDOMNodeList
Set objDOMDocument = New DOMDocument
objDOMDocument.async = False
objDOMDocument.Load "http://localhost/xmlcode/people2.dtd"
Set objXMLDOMNodeList = objDOMDocument.documentElement.childNodes
<?xml version="1.0" ?>
<!-- *********** Resumes for People *********** -->
<!DOCTYPE PEOPLE SYSTEM "http://localhost/xmlcode/people.dtd">
<PEOPLE>
<PERSON id="1">
<NAME>Mark Wilson</NAME>
<ADDRESS>911 Somewhere Circle, Canberra, Australia</ADDRESS>
<TEL>(++612) 12345</TEL>
<FAX>(++612) 12345</FAX>
<EMAIL>markwilson@somewhere.com</EMAIL>
</PERSON>
<PERSON id="2">
<NAME>Tracey Wilson</NAME>
<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
childNodesBecause 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
The objNode has been declared as an IXMLDOMElement.
if objPeopleRoot.length > 0 then
...
end if    
This manuscript is an abridged version of a chapter
from the Manning
Publications book XML
Programming 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!
|