Solved

VB 6.0 XML Trouble!

Posted on 2009-04-08
4
426 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you very much robinu! It's working perfectly!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Removing information on Duplicate XML Nodes 24 66
Excel object stays open 19 64
passing parameter in sql procedure 9 50
XSLT Help 12 19
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

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

9 Experts available now in Live!

Get 1:1 Help Now