Solved

How do I parse XML in VB - part 2

Posted on 2011-03-23
2
297 Views
Last Modified: 2012-05-11
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!
0
Comment
Question by:mph23
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35196804
This:
xmlchild.selectSingleNode("//value").Text

Open in new window

Should say:
xmlchild.selectSingleNode(".//value").Text

Open in new window

The // says "search anywhere from the root of the document. Putting the dot in front says "search anywhere below the context node. Of course, it would process faster is you used the more explicit:
xmlchild.selectSingleNode("reportDataList/reportData/value").Text

Open in new window

0
 

Author Closing Comment

by:mph23
ID: 35197576
Thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question