Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' 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
- Public 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement