excel - VBA XML parsing - looping through child nodes -
this first attempt @ parsing xml files using vba, may missing obvious; can print this:
<values> <value code="1">a</value> <value code="2">b</value> <value code="3">c</value> </values>
using code line:
debug.print variable.selectsinglenode("values").xml
, values
child node of parent variable
but can't figure out how loop through values
's children, , print "1a", "2b", "3c" pairs
as far can understand, this question uses first child of root, while goal deeper multiple-leveled structure.
here can see how use msxml6.0 library parse xml particular example. use example, need add reference msxml6.0 in vba project.
i suggest pay particular attention xpath variable '//value' , selectors such .getnameditem("code") --- there many more of these need familiarize in order become fluent in xml parsing. fortunately lot of passes on html parsing useful skill!
in case, have selected value nodes. iterating through them simple doing loop based on length of array of nodes , using .item(i) call.
option explicit sub test() dim strxml string strxml = "<values><value code=""1"">a</value><value code=""2"">b</value><value code=""3"">c</value></values>" dim objxml msxml2.domdocument60 set objxml = new msxml2.domdocument60 if not objxml.loadxml(strxml) 'strxml string xml' err.raise objxml.parseerror.errorcode, , objxml.parseerror.reason end if dim entry_point ixmldomnode set entry_point = objxml dim mynodes ixmldomnodelist dim myelement ixmldomelement dim mynode ixmldomnode dim nnode integer set mynodes = entry_point.selectnodes("//value") if mynodes.length > 0 nnode = 0 mynodes.length set mynode = mynodes(nnode) ' first node. if mynode nothing else debug.print mynode.text debug.print mynode.attributes.getnameditem("code").text end if next nnode else debug.print "no nodes found." end if end sub
here case select values nodes , iterate through children of each values node (assuming values nodes have value children).
option explicit sub test() dim strxml string strxml = "<values><value code=""1"">a</value><value code=""2"">b</value><value code=""3"">c</value></values>" dim objxml msxml2.domdocument60 set objxml = new msxml2.domdocument60 if not objxml.loadxml(strxml) 'strxml string xml' err.raise objxml.parseerror.errorcode, , objxml.parseerror.reason end if dim entry_point ixmldomnode set entry_point = objxml dim mynodes ixmldomnodelist dim mychildnodes ixmldomnodelist dim myelement ixmldomelement dim mynode ixmldomnode dim mychildnode ixmldomnode dim nnode integer dim nchildnode integer set mynodes = entry_point.selectnodes("//values") if mynodes.length > 0 nnode = 0 mynodes.length - 1 set mynode = mynodes(nnode) if mynode nothing else set mychildnodes = mynode.childnodes ' children of first node. nchildnode = 0 mychildnodes.length - 1 debug.print mychildnodes(nchildnode).text debug.print mychildnodes(nchildnode).attributes.getnameditem("code").text next nchildnode end if next nnode else debug.print "no nodes found." end if end sub
Comments
Post a Comment