XML DOM » Node » childNodes

Syntax:
node.childNodes

This is a read-only property containing a node list of all children for those elements that can have them.

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.

Examples

Code:
XML:

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

VBScript:

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

Dim objChildNodes, strNode
Set objChildNodes = objXMLDoc.documentElement.childNodes<State>

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

This example loads the 'states.xml' file and gets the childNodes of the root element. 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.

As you might have guessed, each of the child nodes in the above example has its own child nodes.

Language(s): VBScript XML
Code:
Set objChildNodes = objXMLDoc.documentElement.childNodes.item(0).childNodes<State>

For Each strNode In objChildNodes
   document.write(strNode.xml & "<br>")
Next
Output:
Florida
Tallahassee
Explanation:

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 example. Now you can loop through the child nodes representing the elements of the first <State> tag, and display the xml for those nodes:

Language(s): VBScript