excel - VBA - Loop specific childnodes from XML code -


i'm trying scrape following xml excel sheet. however, want loop through specific childnodes show name, priceeffectivestart, priceeffectiveend, price, , currency each index summary.

xml code

<indexprices>      <indexpricesummary>          <id>1</id>          <uri>www.example.com</uri>       <index>         <id>3</id>         <name>same day index</name>         <uri>www.example.com.xml</uri>       </index>       <priceeffectivestart>2015-06-26</priceeffectivestart>       <priceeffectiveend>2015-06-26</priceeffectiveend>       <price>          <amount>2.4806</amount>          <currency>cad</currency>       </price>       <duration>1</duration>       <quantitytraded>         <amount>474</amount>         <unit>gj</unit>         <contractunit>day</contractunit>       </quantitytraded>       <numberoftrades>7</numberoftrades>       <settlementstate>settled</settlementstate>       <lastupdatedate>2015-06-27t02:15:01-06:00</lastupdatedate>     </indexpricesummary>     <indexpricesummary>         <id>1</id>         <uri>www.example.com.xml</uri>      <index>       <id>1</id>       <name>same day index </name>       <uri>www.example.com.xml</uri>      </index>      <priceeffectivestart>2015-06-27</priceeffectivestart>      <priceeffectiveend>2015-06-27</priceeffectiveend>      <price>          <amount>2.516</amount>          <currency>cad</currency>      </price>      <duration>1</duration>      <quantitytraded>         <amount>251</amount>         <unit>gj</unit>         <contractunit>day</contractunit>      </quantitytraded>      <numberoftrades>50</numberoftrades>      <settlementstate>settled</settlementstate>      <lastupdatedate>2015-06-28t02:15:00-06:00</lastupdatedate>    </indexpricesummary> </indexprices> 

vba code

dim xdoc domdocument set xdoc = new domdocument  xdoc.loadxml objhttp.responsetext  dim integer dim list ixmldomnodelist set list = xdoc.selectnodes("//indexprices/indexpricesummary")  dim node ixmldomnode dim childnode ixmldomnode dim price ixmldomnode  each node in list     = + 1      if (node.haschildnodes)         each childnode in node.childnodes              = + 1             debug.print childnode.basename & " " & childnode.text              worksheets("sheet1").cells(i, 1) = childnode.basename              worksheets("sheet1").cells(i, 2) = childnode.text         next childnode     end if     next node 

the current vba shows nodes in output. show name, priceeffectivestart, priceeffectiveend, price, , currency each index summary.

thanks help!

you can use xpath on each indexpricesummary node @ child elements directly:

sub tester()     dim xdoc domdocument     set xdoc = new domdocument      ''more code here       xdoc.loadxml objhttp.responsetext      dim integer     dim list ixmldomnodelist     set list = xdoc.selectnodes("//indexprices/indexpricesummary")      dim node ixmldomnode, nd ixmldomnode     dim childnode ixmldomnode     dim price ixmldomnode      = 4     each node in list         = + 1          sheet1.rows(i)             .cells(1).value = getnodevalue(node, "index/name")             .cells(2).value = getnodevalue(node, "priceeffectivestart")             .cells(3).value = getnodevalue(node, "priceeffectiveend")             .cells(4).value = getnodevalue(node, "price/amount")             .cells(5).value = getnodevalue(node, "price/currency")         end      next node  end sub  function getnodevalue(node ixmldomnode, xp string)     dim n ixmldomnode, nv     set n = node.selectsinglenode(xp)     if not n nothing nv = n.nodetypedvalue     getnodevalue = nv end function 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -