Link to home
Start Free TrialLog in
Avatar of mph23
mph23Flag for United States of America

asked on

How do I parse XML in VB - part 2

I am trying to parse xml data that is in an Access database. When I run this code, I get the same "value" of 10 twice but it should be 10 then 5.

Sub ParseXML(pstrXML As String)
   
    Dim xmlDoc As New MSXML2.DOMDocument
    Dim xmlNode As MSXML2.IXMLDOMElement
       
    Dim iPos As Long
    Dim sOperations(0 To 1) As String
    sOperations(0) = "NumberOfFiles"
    sOperations(1) = "NumberOfFolders"
   
    xmlDoc.loadXML sXML
    For iPos = LBound(sOperations) To UBound(sOperations)
          op = sOperations(iPos)
 
          For Each xmlNode In xmlDoc.childNodes(0).childNodes
             If xmlNode.nodeName = "operations" Then
                For Each xmlchild In xmlDoc.selectNodes("//reportElement [@reportCode='" & op & "']")
                    Debug.Print xmlchild.getAttribute("reportCode") & ": " & xmlchild.selectSingleNode("//value").Text
               Next
             End If
          Next xmlNode

     Next iPos
 
End Sub
-----------------------------------------------
Sample XML:

        "<operations><operation> " & _
        "<report> " & _
        "<reportElementList> " & _
        "<reportElement type='Info' reportCode='NumberOfFiles' dateTime='12/9/2010 8:06:21 PM'> " & _
        "<reportDataList> " & _
        "<reportData key='Amount'> " & _
        "<value>10</value> " & _
        "</reportData> " & _
        "</reportDataList> " & _
        "</reportElement> " & _
        "<reportElement type='Info' reportCode='NumberOfFolders' dateTime='12/9/2010 8:06:21 PM'> " & _
        "<reportDataList> " & _
        "<reportData key='Amount'> " & _
        "<value>5</value> " & _
        "</reportData> " & _
        "</reportDataList> " & _
        "</reportElement> " & _
        "</reportElementList> " & _
        "</report></operation> " & _
        "</operations></operation>"
------------------------------------------

Desired Results:
NumberOfFiles: 10
NumberOfFolders: 5

Erroneous Results I'm getting::
NumberOfFiles: 10
NumberOfFolders: 10


Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mph23

ASKER

Thanks!