VB 6.0 XML Trouble!

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

vfafouAsked:
Who is Participating?
 
RobinConnect With a Mentor Software EngineerCommented:
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
 
RobinSoftware EngineerCommented:
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
 
vfafouAuthor Commented:
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
 
vfafouAuthor Commented:
Thank you very much robinu! It's working perfectly!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.