ZetCode

VBScript childNodes Property

last modified April 9, 2025

The childNodes property in VBScript is part of the XML DOM implementation. It returns a collection of all child nodes of the specified node. This property is essential for navigating and manipulating XML documents. It provides access to both element nodes and text nodes within the hierarchy.

childNodes is read-only and returns a NodeList object. The collection includes all direct children regardless of node type. This tutorial covers childNodes with practical examples to demonstrate XML processing in VBScript.

childNodes Property Overview

The childNodes property belongs to the XML DOM Node interface. It provides access to all immediate children of a node. The returned NodeList is live, updating automatically as the document changes. Nodes are indexed starting from 0.

Important aspects include handling whitespace text nodes and element nodes. The property works with both loaded XML documents and dynamically created nodes. Understanding childNodes is fundamental for XML processing.

Basic XML Document Traversal

This example demonstrates basic traversal of an XML document using childNodes. It shows how to access and display child nodes. The script loads a simple XML string and examines its structure.

basic_traversal.vbs
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.loadXML "<root><item>First</item><item>Second</item></root>"

Set root = xmlDoc.documentElement
For Each child In root.childNodes
    WScript.Echo child.nodeName & ": " & child.text
Next

Set xmlDoc = Nothing

The script creates an XML document with two item elements. It accesses the root element's childNodes collection. Each child node's name and text content are displayed. This shows basic XML navigation.

Counting Child Nodes

This example demonstrates using the childNodes length property. It shows how to determine the number of children a node contains. The count includes all node types, not just elements.

count_nodes.vbs
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.loadXML ">books>>book/>>book/>>book/>>/books>"

Set books = xmlDoc.documentElement
WScript.Echo "Number of book elements: " & books.childNodes.length

Set xmlDoc = Nothing

The script loads an XML document with three book elements. It accesses the childNodes.length property of the books element. The output shows the count of direct child nodes. This is useful for validation.

Accessing Specific Child Nodes

This example shows how to access specific nodes using their index in the childNodes collection. It demonstrates zero-based indexing and direct node access. The script retrieves and displays a particular child.

specific_node.vbs
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.loadXML "<colors><color>Red</color><color>Green</color><color>Blue</color></colors>"

Set colors = xmlDoc.documentElement
Set secondColor = colors.childNodes(1)
WScript.Echo "Second color: " & secondColor.text

Set xmlDoc = Nothing

The script loads an XML document with three color elements. It accesses the second color using index 1 (zero-based). The text content of the specific node is displayed. This shows targeted node access.

Handling Different Node Types

This example demonstrates working with different node types in the childNodes collection. It shows how to identify and process element nodes versus text nodes. The script examines each child's node type.

node_types.vbs
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.loadXML "<data>Text content<item>Value</item></data>"

Set dataNode = xmlDoc.documentElement
For Each child In dataNode.childNodes
    Select Case child.nodeType
        Case 1: WScript.Echo "Element: " & child.nodeName
        Case 3: WScript.Echo "Text: " & child.nodeValue
    End Select
Next

Set xmlDoc = Nothing

The script loads XML containing both text and element nodes. It checks each child's nodeType property. Different processing occurs based on whether the node is an element or text. This shows type-aware processing.

Modifying Child Nodes

This example demonstrates modifying nodes within the childNodes collection. It shows how to change node content and structure. The script updates a specific child node's value.

modify_nodes.vbs
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.loadXML "<settings><option>Default</option></settings>"

Set settings = xmlDoc.documentElement
settings.childNodes(0).text = "Custom"
WScript.Echo "Modified option: " & settings.childNodes(0).text

Set xmlDoc = Nothing

The script loads an XML document with one option element. It accesses the first child node and modifies its text content. The change is then verified by displaying the updated value. This shows dynamic XML modification.

Source

XML DOM Documentation

In this article, we have explored the childNodes property in VBScript, covering its usage and practical applications. From basic traversal to node modification, these examples demonstrate XML processing techniques. With this knowledge, you can effectively work with XML documents in VBScript.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all VBScript tutorials.