PROPERTY:  Object::childNodes

Object  Attr   CDATASection   CharacterData   Comment   Document   DocumentFragment   DocumentType   Entity   EntityReference   Node   Notation   ProcessingInstruction   Text
 
Object.childNodes

The childNodes property is a read-only property containing a node list of all children for those elements that can have them. It returns a NodeList for the following valid node types:

  • NODE_ATTRIBUTE
  • NODE_DOCUMENT
  • NODE_DOCUMENT_FRAGMENT
  • NODE_ELEMENT
  • NODE_ENTITY
  • NODE_ENTITY_REFERENCE

The DocumentType node also returns a NodeList object, but this can include entities and notations. If there are no children, or the node is of an invalid type, then the length of the list will be set to 0.

The following example loads the 'states.xml' file and gets the childNodes of the root element.

XML:
<States>
   <State ref="FL">
      <name>Florida</name>
      <capital>Tallahassee</capital>
   </State>
   <State ref="IA">
      <name>Iowa</name>
      <capital>Des Moines</capital>
   </State>
</States>

Code (VBScript):
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("states.xml")

Dim objChildNodes, strNode
Set objChildNodes = objXMLDoc.documentElement.childNodes

And if we then iterate through the child nodes, displaying their node names, you will see that they are the two <State> tags of our XML file.

Code:
For Each strNode In objChildNodes
   document.write(strNode.nodeName & "<br>")
Next

Output:
State
State

As you might have guessed, each of the child nodes in the above example has its own child nodes. To refer to a random, individual node in the childNodes collection you use the item method of the NodeList object with the appropriate index. So, if we want to get the child nodes of the first child node of the root element, the code would look like this:

Code:
Set objChildNodes = objXMLDoc.documentElement.childNodes.item(0).childNodes

Now you can loop through the child nodes representing the elements of the first <State> tag, and display the xml for those nodes:

Code:
For Each strNode In objChildNodes
   document.write(strNode.xml & "<br>")
Next

Output:
Florida
Tallahassee



Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information