Solved

How do I parse XML in VB - part 2

Posted on 2011-03-23
2
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

759 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