How to access a specific element and attribute in xml with vba in excel?

前端 未结 1 1414
不思量自难忘°
不思量自难忘° 2021-02-10 09:48

I\'m struggling with getting excel to parse through an xml file. I\'ve found a plethora of examples, but none seem to be quite what I am looking for and I can\'t seem to get pas

相关标签:
1条回答
  • 2021-02-10 10:27

    I think that the final code presented in the question may not always traverse the whole xml document as the xmlChildren variable is overridden during the loop so I think this may just get the first child node and it's first child and so on.

    To traverse the whole document you can call a separate procedure and build in a recursive call so that it will follow each of the child nodes but then return back to the list it was called from when done.

    Here is a simplified example where I am looking for all instances of a specific xml element, say all carts in an xml doc containing:

    <?xml version="1.0" encoding="UTF-8"?>
    <ImportConfig>
        <ShoppingCarts description="Any carts added here will be picked up by the auto import">
            <cart>shopping cart 1 name here</cart>
            <cart>shopping cart 2 name here</cart>
        </ShoppingCarts>
    </ImportConfig>
    

    The first procedure below is specific for this example i.e. where the tags are named etc, but the other two can be used generically for any xml document (the first is just an example of how they can be used):

    ' Chris Prosser 09/07/2014
    ' example use of getElementList (in this case to get all cart elements)
    
    Sub getCarts()
        Dim carts As Collection
        Dim i As Integer
    
        Set carts = New Collection
        getElementList "C:\Users\Chris\Dropbox\VBAutomation\AutoImportConfig.xml", "cart", carts
    
        For i = 1 To carts.count
            Debug.Print carts.Item(i)
        Next
    
    End Sub
    
    ' Chris Prosser 09/07/2014
    ' Gets the values of all instances of a specific element from an xml file
    
    Sub getElementList(xml_file_path As String, _
                              elementName As String, _
                              elementValuesList As Collection)
    
        Dim xmlDoc As MSXML2.DOMDocument
        Dim xmlRoot As MSXML2.IXMLDOMNode
        Dim xmlChildren As MSXML2.IXMLDOMNodeList
        Dim xmlElement As MSXML2.IXMLDOMElement
    
        Set xmlDoc = New MSXML2.DOMDocument
        xmlDoc.async = False
        xmlDoc.validateOnParse = False
        xmlDoc.Load (xml_file_path)
        Set xmlRoot = xmlDoc.documentElement
        Set xmlChildren = xmlRoot.childNodes
    
        iterateOverChildNodes xmlChildren, elementName, elementValuesList
    
    End Sub
    
    ' Chris Prosser 09/07/2014
    ' Call with a list of xmlNodes (can be generated from a file using getElementList)
    ' and an element name to search for. The procedure find child nodes and re-runs
    ' recursively until all branchs from the list of nodes passed in have been traversed
    
    Sub iterateOverChildNodes(xmlChildren As MSXML2.IXMLDOMNodeList, _
                              elementName As String, _
                              elementValuesList As Collection)
    
        Dim xmlElement As MSXML2.IXMLDOMElement
        Dim xmlGrandChildren As MSXML2.IXMLDOMNodeList
    
        For Each xmlElement In xmlChildren
            If xmlElement.nodeName = elementName Then
                'Debug.Print xmlElement.nodeTypedValue
                elementValuesList.Add xmlElement.nodeTypedValue
            Else
                Set xmlGrandChildren = xmlElement.childNodes
                iterateOverChildNodes xmlGrandChildren, elementName, elementValuesList
            End If
        Next xmlElement
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题