Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB 6.0 XML Trouble!

Posted on 2009-04-08
4
Medium Priority
?
458 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
[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
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Robin
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:
Robin earned 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Suggested Courses

610 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