VBScript MSXML2.IXMLDOMNode Object
last modified April 9, 2025
The MSXML2.IXMLDOMNode object in VBScript represents a single node
in an XML document tree. It is part of the Microsoft XML Core Services (MSXML)
library. This object provides methods and properties to manipulate XML nodes.
It serves as the base interface for all other XML DOM node types.
IXMLDOMNode enables reading, modifying, adding, and deleting XML
nodes. It supports navigation through the XML document structure. This tutorial
covers IXMLDOMNode with practical examples to demonstrate its usage.
IXMLDOMNode Object Overview
The IXMLDOMNode interface is the fundamental building block of XML
DOM. It provides core functionality shared by all node types. Properties include
nodeName, nodeValue, and childNodes.
Key methods include appendChild, removeChild, and
selectNodes. The object supports XPath expressions for node
selection. Understanding this interface is essential for XML processing in
VBScript.
Accessing Node Properties
This example demonstrates accessing basic properties of an XML node. It shows how to retrieve the node name, value, and type. The script loads a simple XML string and examines its root node.
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.loadXML "<book><title>VBScript Guide</title></book>"
Set root = xmlDoc.documentElement
WScript.Echo "Node name: " & root.nodeName
WScript.Echo "Node type: " & root.nodeType
WScript.Echo "XML content: " & root.xml
Set xmlDoc = Nothing
The script creates an XML document with a book element. It accesses the root
node's properties using nodeName and nodeType. The
xml property returns the node's complete XML content.
Navigating Child Nodes
This example shows how to traverse child nodes of an XML element. It demonstrates accessing the first child node and iterating through all children. The script examines each node's properties.
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.loadXML "<library><book>Title1</book><book>Title2</book></library>"
Set library = xmlDoc.documentElement
Set firstChild = library.firstChild
WScript.Echo "First child name: " & firstChild.nodeName
For Each child In library.childNodes
WScript.Echo "Child node value: " & child.text
Next
Set xmlDoc = Nothing
The script loads an XML document with multiple book elements. It accesses the
first child node using firstChild. Then it iterates through all
children using the childNodes collection.
Modifying Node Content
This example demonstrates modifying XML node content. It shows how to change a node's text value and attributes. The script updates the XML document and outputs the modified content.
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.loadXML "<person><name>John</name><age>30</age></person>"
Set nameNode = xmlDoc.selectSingleNode("//name")
nameNode.text = "Michael"
Set ageNode = xmlDoc.selectSingleNode("//age")
ageNode.text = "35"
WScript.Echo "Modified XML: " & vbCrLf & xmlDoc.xml
Set xmlDoc = Nothing
The script loads an XML document with person data. It uses
selectSingleNode to locate specific nodes. The text content of
both name and age nodes is updated. Finally, the modified XML is displayed.
Adding and Removing Nodes
This example shows how to add new nodes and remove existing ones. It demonstrates creating elements and appending them to the document. The script also shows node removal.
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.loadXML "<employees><employee>Alice</employee></employees>"
Set root = xmlDoc.documentElement
' Add new employee
Set newEmp = xmlDoc.createElement("employee")
newEmp.text = "Bob"
root.appendChild newEmp
' Remove first employee
Set firstEmp = root.firstChild
root.removeChild firstEmp
WScript.Echo "Updated XML: " & vbCrLf & xmlDoc.xml
Set xmlDoc = Nothing
The script starts with an XML document containing one employee. It creates a new
employee node using createElement and appends it. Then it removes
the original employee node. The final XML shows the modifications.
Using XPath to Select Nodes
This example demonstrates using XPath expressions to select nodes. It shows how
to retrieve specific nodes based on criteria. The script uses both
selectNodes and selectSingleNode methods.
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.loadXML "<products><product id='1'>Laptop</product><product id='2'>Phone</product></products>"
' Select single node
Set product1 = xmlDoc.selectSingleNode("//product[@id='1']")
WScript.Echo "Product 1: " & product1.text
' Select multiple nodes
Set products = xmlDoc.selectNodes("//product")
For Each prod In products
WScript.Echo "Product: " & prod.text
Next
Set xmlDoc = Nothing
The script loads an XML document with product data. It uses XPath to select a specific product by ID attribute. Then it selects all product nodes and iterates through them. XPath provides powerful node selection capabilities.
Source
MSXML2.IXMLDOMNode Documentation
In this article, we have explored the MSXML2.IXMLDOMNode object in
VBScript, covering its properties and methods. From basic node access to complex
manipulations, these examples demonstrate XML processing capabilities. With this
knowledge, you can effectively work with XML data in your VBScript applications.
Author
List all VBScript tutorials.