I\'m trying to scrape the following Xml
into an Excel sheet. However, I only want to loop through specific childnodes to show the Name
, PriceEffe
You can use xpath on each indexPriceSummary
node to get at the child elements directly:
Sub Tester()
Dim xDoc As DOMDocument
Set xDoc = New DOMDocument
''more code here
xDoc.LoadXML objHTTP.responseText
Dim i As Integer
Dim list As IXMLDOMNodeList
Set list = xDoc.SelectNodes("//indexPrices/indexPriceSummary")
Dim node As IXMLDOMNode, nd As IXMLDOMNode
Dim childNode As IXMLDOMNode
Dim price As IXMLDOMNode
i = 4
For Each node In list
i = i + 1
With 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 With
Next node
End Sub
Function GetNodeValue(node As IXMLDOMNode, xp As String)
Dim n As IXMLDOMNode, nv
Set n = node.SelectSingleNode(xp)
If Not n Is Nothing Then nv = n.nodeTypedValue
GetNodeValue = nv
End Function