I have the an XML table similar to the following... and I want to extract all the fields in update when the name is what I say... so basicaly if I say, name="test2", I want 1,2 to come back, if I say name="test" I want nothing back, and if I say name="test3" I want just 1.
I then use the VB below that to extract those numbers... but I can't believe this is even close to optimal... could anyone advise me on simplifications? Thanks a ton, even if it's only for reading over all this junk.
-Rob
I then use the VB below that to extract those numbers... but I can't believe this is even close to optimal... could anyone advise me on simplifications? Thanks a ton, even if it's only for reading over all this junk.
-Rob
Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
<product>
<name>test</name>
</product>
<product>
<name>test2</name>
<instance>
<number>1</number>
<update>woo</update>
</instance>
<instance>
<number>2</number>
<update>moo</update>
</instance>
</product>
<product>
<name>test3</name>
<instance>
<number>1</number>
<update>foo</update>
</instance>
</product>
</root>
Code:
Option Explicit
Private Sub Form_Load()
Dim xmlDocTest As New MSXML2.DOMDocument30
Dim xmlDoc2 As New MSXML2.DOMDocument30
Dim xmlNode As IXMLDOMNode
Dim xmlNode2 As IXMLDOMNode
Dim strDestination As String
Dim childNode As IXMLDOMNodeList
Dim childNode2 As IXMLDOMNodeList
Dim test As Boolean
Dim i, j, k As Integer
Dim product$
product = "test2"
xmlDocTest.async = False
test = xmlDocTest.Load("[URL unfurl="true"]http://blah/blah.xml")[/URL]
If test = False Then
Debug.Print "Error : " & xmlDocTest.parseError.reason
Debug.Print "On Line: " & xmlDocTest.parseError.Line
Debug.Print "Source : " & xmlDocTest.parseError.srcText
Else
Set childNode = xmlDocTest.getElementsByTagName("name")
For i = 1 To childNode.length
If (childNode.Item(i - 1).Text = product) Then
Debug.Print "---------------------------"
Set xmlNode = childNode.Item(i - 1).parentNode
test = xmlDoc2.loadXML(xmlNode.xml)
Set childNode2 = xmlDoc2.getElementsByTagName("instance")
For j = 1 To childNode2.length
For k = 1 To childNode2.Item(j - 1).childNodes.length
If childNode2.Item(j - 1).childNodes.Item(k - 1).nodeName = "number" Then
Debug.Print childNode2.Item(j - 1).childNodes.Item(k - 1).Text
End If
Next
Next
Debug.Print "---------------------------"
End If
Next