VBA - Loop specific childnodes from XML code

前端 未结 1 1736
花落未央
花落未央 2021-01-21 02:22

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

1条回答
  •  不思量自难忘°
    2021-01-21 02:58

    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
    

    0 讨论(0)
提交回复
热议问题