VB Script example to read soap message

by Jeff Fillegar on May 15, 2010

VB Script example to read soap message…

Task: Read the following xml using vbs and return the count of how many flights and the price and flight number from each flight element.

This is sample xml:
<s:Envelope xmlns:s=”http://schemas.xmlsoap.org/soap/envelope/“>
            <GetFlightsResponse xmlns=”HP.SOAQ.SampleApp”>
                  <GetFlightsResult xmlns:i=”http://www.w3.org/2001/XMLSchema-instance“>
                              <ArrivalTime>04:17 PM</ArrivalTime>
                              <DepartureCity>Los Angeles</DepartureCity>
                              <DepartureTime>02:22 PM</DepartureTime>
                              <ArrivalTime>12:41 PM</ArrivalTime>
                              <DepartureCity>Los Angeles</DepartureCity>
                              <DepartureTime>10:46 AM</DepartureTime>
                              <ArrivalTime>11:29 AM</ArrivalTime>
                              <DepartureCity>Los Angeles</DepartureCity>
                              <DepartureTime>09:34 AM</DepartureTime>

Using VBscript and the XML DOM object, this is how it can be read and return:
1) Count of how many flight elements
2) Flight number
3) Price

Dim strXMLDoc
Dim objXMLDoc, colNodes, objNode, objChildNode
Dim strFlightNum, strPrice, strAirline
Dim strOutput

strXMLDoc = “c:\temp\flights.xml”

Set objXMLDoc = CreateObject(“MSXML.DOMDocument”)
objXMLDoc.Async = False

If (Not objXMLDoc.Load(strXMLDoc)) Then
  msgbox “Error loading XML file ‘” & strXMLDoc & “‘.” & vbCrLf & _
               “Error code: 0x” & Hex(objXMLDoc.ParseError.ErrorCode) & vbCrLf & _
               “Description: ” & objXMLDoc.ParseError.Reason & vbCrLf & _
               “Line: ” & objXMLDoc.ParseError.Line & vbCrLf & _
               “Character: ” & objXMLDoc.ParseError.LinePos
      ‘stop qtp script
End If

‘set properties of xml object
objXMLDoc.SetProperty “SelectionLanguage”, “XPath”
objXMLDoc.SetProperty “SelectionNamespaces”, _
  “xmlns:s=’http://schemas.xmlsoap.org/soap/envelope/’ ” & _

objXMLDoc.SetProperty “SelectionNamespaces”,  “xmlns:s=’http://schemas.xmlsoap.org/soap/envelope/’ xmlns:r=’HP.SOAQ.SampleApp'”

‘set xpath
Set colNodes = objXMLDoc.SelectNodes(“/s:Envelope/s:Body/r:GetFlightsResponse/r:GetFlightsResult/r:Flight”)

‘count the number of flights that return
‘number of flights that return is something that we want to validate as well
strOutput = “Number of flights: ” & colNodes.Length & vbCrLf & vbCrLf
msgbox strOutput

‘loop thru the nodes
For Each objNode In colNodes
  strFlightNum = “*Not Found*”
  strPrice = “*Not Found*”
  strAirline = “*Not Found*”

  Set objChildNode = objNode.SelectSingleNode(“r:FlightNumber”)
  If (Not objChildNode Is Nothing) Then
        strFlightNum = objChildNode.Text
  End IF

  Set objChildNode = objNode.SelectSingleNode(“r:Airlines”)
  If (Not objChildNode Is Nothing) Then
        strAirline = objChildNode.Text      �
  End IF

  Set objChildNode = objNode.SelectSingleNode(“r:Price”)
  If (Not objChildNode Is Nothing) Then
        strPrice = FormatNumber(objChildNode.Text)
  End IF

  strOutput = strOutput & “Flight ‘” & strAirline & ” ” & strFlightNum & “‘ price: ” & strPrice & vbCrLf
  ‘msgbox strOutput


Set objXMLDoc = nothing

Leave a Comment

Previous post:

Next post: