VBScript : How to iterate dynamically to subnodes of xml file using vbscript -
this question has answer here:
- recurse xml file using vbscript 2 answers
i have xml file root node has child node , child node may have it's own child node in turn have other child node , on. child1 child of parent , child2 child of child1 , on
structure follows
<parent> <child1> <child2> <child3> </child3> </child2> </child1> </parent>
the problem don't know level of nesting of nodes i.e. number of child nodes within 1 child node want dynamically read nodes xml file
currently using vbscript code
set x=xmldoc.documentelement msgbox "nodename: " & x.nodename & vbnewline set y=x.childnodes i=0 y.length-1 msgbox "nodename: " & y(i).nodename & vbnewline z=0 y(i).childnodes.length-1 msgbox "nodename: " & y(i).childnodes(z).nodename & vbnewline next next
but level of nesting required know in advance , accordingly number of loop required. example:above code can read upto child1 reading child2, requires add 1 more loop , child3 1 more loop required. can me?
following comments above, want simple recursive function below should give starting point.
dim xml: xml = "<parent><child1><child2><child3></child3></child2></child1></parent>" dim xmldoc : set xmldoc = createobject("msxml2.domdocument.6.0") if xmldoc.loadxml(xml) call mapnodes(xmldoc) end if 'recursive sub procedure takes node input. sub mapnodes(currentnode) dim node if isobject(currentnode) if currentnode.haschildnodes() each node in currentnode.childnodes wscript.echo "<" & node.nodename & ">" 'do have childnodes? call procedure again time 'passing node checking. if node.haschildnodes() call mapnodes(node) wscript.echo "</" & node.nodename & ">" next end if end if end sub
output:
<parent> <child1> <child2> <child3> </child3> </child2> </child1> </parent>
Comments
Post a Comment