Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I parse XML in VB - part 2

Posted on 2011-03-23
2
Medium Priority
?
328 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

885 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