Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simplify my use of DOM

Status
Not open for further replies.

skiflyer

Programmer
Sep 24, 2002
2,213
US
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

Code:
<?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?>
<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 = &quot;test2&quot;

xmlDocTest.async = False
test = xmlDocTest.Load(&quot;[URL unfurl="true"]http://blah/blah.xml&quot;)[/URL]
If test = False Then
    Debug.Print &quot;Error  : &quot; & xmlDocTest.parseError.reason
    Debug.Print &quot;On Line: &quot; & xmlDocTest.parseError.Line
    Debug.Print &quot;Source : &quot; & xmlDocTest.parseError.srcText
Else
    
Set childNode = xmlDocTest.getElementsByTagName(&quot;name&quot;)
For i = 1 To childNode.length
    If (childNode.Item(i - 1).Text = product) Then
        Debug.Print &quot;---------------------------&quot;
        Set xmlNode = childNode.Item(i - 1).parentNode
        test = xmlDoc2.loadXML(xmlNode.xml)
        Set childNode2 = xmlDoc2.getElementsByTagName(&quot;instance&quot;)
        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 = &quot;number&quot; Then
                    Debug.Print childNode2.Item(j - 1).childNodes.Item(k - 1).Text
                End If
            Next
        Next
        Debug.Print &quot;---------------------------&quot;
    End If
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top