Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to traverse all XML nodes with VBScript

Status
Not open for further replies.

ZarnyWhoop

Programmer
Dec 15, 2009
1
GB
I have written a VBScript to traverse all nodes in an XML file irrespective of the number of levels to which the tree goes. It returns all the values but I need all the node names as well so I have name/value pairs I can use elsewhere. For any nodes which are level 2 deep or more (that means below the "Generic" node in my XML file), no node name are returned. Values yes but not names. Can anyone please help me.

My script is this:

<html>
<head>
</head>
<body>

<script type="text/vbscript">
Set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("test.xml")
Dim objDocElem, strNode, strSubNode, xmlnn, xmlnv, xmlnc, xmldd, xmlfd, xmlfv

Set n_firstchild = xmldoc.documentElement.firstChild
Set p_node = n_firstchild.parentNode
Set pn_attribs = p_node.attributes
For Each pnAttr in pn_attribs
xmlfd = xmlfd & pnAttr.name & chr(9)
xmlfv = xmlfv & pnAttr.value & chr(9)
Next

Set objDocElem=xmlDoc.documentElement

Set y = objDocElem.childNodes
i=0
Do While i < y.length
If y(i).nodeType <> 3 Then
If Isnull(y(i).childNodes(0).nodeValue) Then
xmlnv = xmlnv & "Category" & chr(9)
Else
xmlnv = xmlnv & y(i).childNodes(0).nodeValue & chr(9)
End If
xmlnn = xmlnn & y(i).nodeName & chr(9)
xmlnc = xmlnc + 1
z=0
Do While z < y(i).childNodes.length
If y(i).childNodes(z).nodeType <> 3 Then
xmlnc = xmlnc + 1
xmlnn = xmlnn & y(i).childNodes(z).nodeName & chr(9)
xmlnv = xmlnv & y(i).childNodes(z).text & chr(9)
End If
z=z+1
Loop
End If
i=i+1
Loop

document.write("form details: " & xmlfd & "<br />")
document.write("form values: " & xmlfv & "<br /><br />")
document.write("node names: " & xmlnn & "<br />")
document.write("node values: " & xmlnv & "<br />")
document.write("<br />node count: " & xmlnc & "<br />")

</script>
</body>
</html>

And my XML file contains this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<form penid="AJX-AAE-CRB-7P" submitted="2009-09-10 14:57:07" updated="2009-09-10 15:05:22" finalised="2009-09-10 15:59:48">
<Forename>Russell</Forename>
<Surname>Kilby</Surname>
<HouseNumber>248</HouseNumber>
<Letter>GRG</Letter>
<Products>
<WinSoftware>Product One</WinSoftware>
<MacSoftware>Product Two</MacSoftware>
<LnxSoftware>Product Three</LnxSoftware>
<Generic>
<Product1>Speedo</Product1>
<Product2>Switches</Product2>
<Product3>BIOS</Product3>
<TestOne>
<Panel1>Front</Panel1>
<Panel2>Back</Panel2>
<Panel3>Middle</Panel3>
</TestOne>
</Generic>
<Hardware>Fender</Hardware>
</Products>
<HouseName>Just Tea House</HouseName>
<PostCode>B87 7DF</PostCode>
<Insurer>ABC Cars</Insurer>
<PolicyNumber>FDA 8D3JD7K</PolicyNumber>
<Make>Ford</Make>
<VehicleReg>EX51 CBA</VehicleReg>
<DescriptionOfDamage>Big smash on the from</DescriptionOfDamage>
<Estimate>1300</Estimate>
<AlertManager>Yes</AlertManager>
</form>

Cheers, Rob.
 
[1]
>I have written a VBScript to traverse all nodes in an XML file irrespective of the number of levels to which the tree goes.
But, this cannot be done without proper use of recursion of some kind. So what you've done cannot be properly said to have achieving the goal irrespective of the number of levels.

[2] Here is a quick re-work of what you've posted. The format of display cannot be said satisfactory, but that's what you seem happy at present to work with. It replaces the part starting from objDocElem until the document.write.
[tt]
Set objDocElem=xmlDoc.documentElement

Set y = objDocElem.childNodes
i=0
xmlnn=""
xmlnv=""
xmlnc=0
Do While i < y.length
proc y(i), xmlnn, xmlnv, xmlnc
i=i+1
Loop

sub proc(node,sn_inout, sv_inout, nc_inout)
select case node.nodetype
case 1
sn_inout=sn_inout & node.nodename & chr(9)
nc_inout=nc_inout+1
for each x in node.childnodes
proc x, sn_inout, sv_inout, nc_inout
next
case 3
if Isnull(node.nodeValue) Then
sv_inout=sv_inout & "Category" & chr(9)
else
sv_inout=sv_inout & node.nodeValue & chr(9)
end if
case else
'comment and process-instruction etc are not prcessed
end select
end sub
[/tt]
[4] You set up of n_firstchild and p_node is awkward to say the least. And then objDocElem is the same as p_node! All those lines, I pass by not making further comments on.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top