Solved

How do I parse XML in VB - part 2

Posted on 2011-03-23
2
274 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
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now