Friday, January 22, 2010

Accessing node name and node type of all nodes from XML document.

Example 2: Accessing node name and node type of all nodes from XML document.

XML file: Save the below text in a notepad and save it as b.xml.



VBScript code: Save the below text in a notepad and save it as b.vbs.

Creating XMLDOM object and Loading it. The async property specifies whether downloading of an XML file should be handled asynchronously or not. True means that the load() method returns the control to the caller before the download is complete. False means that the download must be completed before the caller gets the control back.
set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async=false
xmlDoc.load("c:\b.xml")

The nodeName, nodeValue, and nodeType properties contain information about nodes.
msgbox("Nodename: " & xmlDoc.nodeName & vbNewLine & "nodetype: " & xmlDoc.nodeType)

set x=xmlDoc.documentElement

msgbox("Nodename: " & x.nodeName & vbNewLine & "nodetype: " & x.nodeType)

set y=x.childNodes

for i=0 to y.length-1
a=a & " " & "Nodename: " & y(i).nodeName & " " & "nodetype: " & y(i).nodeType & vbNewLine

for z=0 to y(i).childNodes.length-1
b=b & " " & "Nodename: " & y(i).childNodes(z).nodeName & " " & "nodetype: " & y(i).childNodes(z).nodeType & vbNewLine
next

next
msgbox a
msgbox b


Now go to command prompt and run this above b.vbs file. Make sure the location of xml file in the b.vbs file (3rd line) is correct.

Following are the output message boxes for this particular code run.









The DOM presents a document as a hierarchy of node objects.

There are many different Node Types: Document (Represents the entire document (the root-node of the DOM tree)),DocumentFragment, DocumentType, ProcessingInstruction, EntityReference, Element, Attr, Text, CDATASection, Comment (Represents a comment), Entity, Notation.

Also See:
Explain XML and XML DOM
VBScript to access number of child nodes and text/nodename of each child node from XML file.
VBScript to add a new node in XML document.
VBScript to append a string at the end of a text node in XML file.
VBScript to compare two XML files and show the differences in a messagebox.