Solved

VB 6.0 XML Trouble!

Posted on 2009-04-08
4
437 Views
Last Modified: 2013-11-10
Hello!

I have the following XML string:

<RouteResponse>
     <RouteResult routeId="1" routingFunctionTime="0.016">
         <RouteSegments>
              <RouteSegment order="1"
                                         name="Route Point 1"
                                         time="0.00"
                                         totalTime="0.00"
                                         distance="0.00"
                                         totalDistance="0.00"
                                         speed="0.00"
                                         turn="-1.00"
                                         turnPointX="0.000000"
                                         turnPointY="0.000000"
                                         isInputPoint="1"/>
              <RouteSegment order="2"
                                         name="NAME1"
                                         time="0.70"
                                         totalTime="0.70"
                                         distance="0.23"
                                         totalDistance="0.23"
                                         speed="20.70"
                                         turn="38.00"
                                         turnPointX="469163.315092"
                                         turnPointY="4203923.500277"
                                         isInputPoint="0"/>
              <RouteSegment order="3"
                                         name="NAME2"
                                         time="0.60"
                                         totalTime="1.30"
                                         distance="0.22"
                                         totalDistance="0.45"
                                         speed="20.30"
                                         turn="24.00"
                                         turnPointX="468949.006187"
                                         turnPointY="4203922.530092"
                                         isInputPoint="0"/>
              <RouteSegment order="4"
                                         name="NAME3"
                                         time="0.30"
                                         totalTime="1.60"
                                         distance="0.06"
                                         totalDistance="0.51"
                                         speed="13.70"    
                                         turn="274.00"
                                         turnPointX="468919.000349"
                                         turnPointY="4203871.500612"
                                         isInputPoint="0"/>
              <RouteSegment order="5"
                                         name="Route Point 2"
                                         time="0.00"
                                         totalTime="1.60"
                                         distance="0.00"
                                         totalDistance="0.51"
                                         speed="0.00"
                                         turn="-3.00"
                                         turnPointX="0.000000"
                                         turnPointY="0.000000"
                                         isInputPoint="1"/>
          </RouteSegments>
        <RouteOrder>
      <RoutePoint pointX="469371.430000" pointY="4203820.750000" description="Route Point 1" timeToStay="0.00"/>
    <RoutePoint pointX="468676.419000" pointY="4204081.986000" description="Route Point 2" timeToStay="0.00"/>
  </RouteOrder></RouteResult>
</RouteResponse>

The class I use for XML is in the code section. I want to make a method to return the values of the fields of the RouteSegments, for example

<RouteSegment order="5"
    name="Route Point 2"
    time="0.00"
    totalTime="1.60"
    distance="0.00"
    totalDistance="0.51"
    speed="0.00"
    turn="-3.00"
    turnPointX="0.000000"
    turnPointY="0.000000"
    isInputPoint="1"/>

May you help me in writing this code?
Thank you in advance!
Option Explicit
 
Dim xDoc As MSXML.DOMDocument
 
Public Enum OpenXMLDoc
    oxFile = 0
    oxString = 1
End Enum
 
Public Enum DocInfoConst
    diVERSION = 0
    diENCODING = 1
    diSTANDALONE = 2
End Enum
 
Public Property Get XMLDocumentInfo(diDesiredInfo As DocInfoConst) As Variant
Dim strDesiredInfo As String
Select Case diDesiredInfo
    Case diVERSION
        strDesiredInfo = "version"
    Case diENCODING
        strDesiredInfo = "encoding"
    Case diSTANDALONE
        strDesiredInfo = "standalone"
End Select
 
Dim xNode As MSXML.IXMLDOMNode
Set xNode = xDoc.childNodes(0).Attributes.getNamedItem(strDesiredInfo)
 
XMLDocumentInfo = xNode.Text
Set xNode = Nothing
End Property
 
Public Function OpenXML(strSource As String, Optional oType As OpenXMLDoc = oxFile) As Boolean
Set xDoc = New MSXML.DOMDocument
 
Select Case oType
    Case oxFile
        OpenXML = xDoc.Load(strSource)
    Case oxString
        OpenXML = xDoc.loadXML(strSource)
End Select
End Function
 
Public Function ReadNode(strQuery As String) As String
On Error GoTo ErrHandle:
 
Dim xNode As MSXML.IXMLDOMNode
Set xNode = xDoc.documentElement.selectSingleNode(strQuery)
 
ReadNode = xNode.Text
 
Set xNode = Nothing
 
Exit Function
ErrHandle:
ReadNode = Null
Set xNode = Nothing
End Function
 
Public Function ReadNodeXML(strQuery As String) As String
Dim xNode As MSXML.IXMLDOMNode
Set xNode = xDoc.documentElement.selectSingleNode(strQuery)
 
ReadNodeXML = xNode.XML
 
Set xNode = Nothing
End Function
 
Public Property Get XML() As String
XML = xDoc.XML
End Property
 
Public Function WriteNode(strQuery As String, Value As Variant)
On Error GoTo ErrHandle:
 
Dim xNode As MSXML.IXMLDOMNode
Set xNode = xDoc.documentElement.selectSingleNode(strQuery)
 
xNode.Text = Value
 
Set xNode = Nothing
 
Exit Function
ErrHandle:
Set xNode = Nothing
End Function
 
Public Function ReadAttribute(strQuery As String, strName As String) As String
On Error GoTo ErrHandle:
 
Dim xNode As MSXML.IXMLDOMElement
Set xNode = xDoc.documentElement.selectSingleNode(strQuery)
 
ReadAttribute = xNode.getAttribute(strName)
 
Set xNode = Nothing
 
Exit Function
ErrHandle:
ReadAttribute = Null
Set xNode = Nothing
End Function
 
Public Function NodeCount(strQuery As String) As Long
On Error GoTo ErrHandle:
 
Dim xNode As MSXML.IXMLDOMNodeList
Set xNode = xDoc.documentElement.selectNodes(strQuery)
 
NodeCount = xNode.Length
 
Set xNode = Nothing
 
Exit Function
ErrHandle:
NodeCount = -1
Set xNode = Nothing
End Function

Open in new window

0
Comment
Question by:vfafou
  • 2
  • 2
4 Comments
 
LVL 16

Expert Comment

by:robinu
ID: 24101312
To keep it simple, use the following. You can easily extend this.

Add the following function to your class:


Public Function SelectNodes(ByVal strQuery As String) As MSXML.IXMLDOMNodeList
    On Error GoTo ErrHandle:
   
    Set SelectNodes = xDoc.documentElement.SelectNodes(strQuery)
    Exit Function
ErrHandle:
    Set SelectNodes = Nothing
End Function


And use this code e.g.:
    Dim l_cXml As clsXML
    Dim oNodeLst As MSXML.IXMLDOMNodeList
    Dim oNode As MSXML.IXMLDOMNode
    Dim oAttr As MSXML.IXMLDOMNode
    
    Set l_cXml = New clsXML
    Call l_cXml.OpenXML("test.xml")
    
    Set oNodeLst = l_cXml.SelectNodes("//RouteSegment")
    If Not (oNodeLst Is Nothing) Then
        For Each oNode In oNodeLst
            For Each oAttr In oNode.Attributes
                Debug.Print oAttr.baseName & "=" & oAttr.Text
            Next
            Debug.Print "---"
        Next
    End If

Open in new window

0
 

Author Comment

by:vfafou
ID: 24101579
Thank you robinu!

It works fine! What if I need to extract only the attribute of the segment with name = "Route Point 2" e.g.

<RouteSegment order="5"
    name="Route Point 2"
    time="0.00"
    totalTime="1.60"
    distance="0.00"
    totalDistance="0.51"
    speed="0.00"
    turn="-3.00"
    turnPointX="0.000000"
    turnPointY="0.000000"
    isInputPoint="1"/>

?
0
 
LVL 16

Accepted Solution

by:
robinu earned 500 total points
ID: 24101847
You can do this by changing the line

Set oNodeLst = l_cXml.SelectNodes("//RouteSegment")

to

Set oNodeLst = l_cXml.SelectNodes("//RouteSegment[@name=""Route Point 2""]")


For more information about XPath, have a look at e.g. http://www.w3schools.com/Xpath/
0
 

Author Closing Comment

by:vfafou
ID: 31568219
Thank you very much robinu! It's working perfectly!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Title # Comments Views Activity
xpath sql query 2008 8 58
what are list of ebay api errors 1 28
checkbox to hide entire section 10 41
Collapse and expand table in Word 2010 2 32
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

839 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