Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RETRIEVE DATA FROM XML RESPONSE INTO ASP VARIABLES/ARRAY

Status
Not open for further replies.

panini

MIS
Jun 1, 2001
136
GB
Hi There,

I have the following object which makes an XML request to a remote server:


requestStr = "<API_Request System=""XXX"" Version=""1.0"" Product=""CarPark"" Customer=""A"" Session=""000000001"" RequestCode=""1"">"

requestStr = requestStr & "<Agent>"
requestStr = requestStr & "<ABTANumber>XXXXX</ABTANumber>"
requestStr = requestStr & "<Password></Password>"
requestStr = requestStr & "<Initials>XXX</Initials>"
requestStr = requestStr & "</Agent>"

requestStr = requestStr & "<Itinerary>"
requestStr = requestStr & "<ArrivalDate>" & st1 & dateMonthText(st2) & right(st3,2) & "</ArrivalDate>"
requestStr = requestStr & "<DepartDate>" & en1 & dateMonthText(en2) & right(en3,2) & "</DepartDate>"
requestStr = requestStr & "<ArrivalTime>" & arrHours & arrMins & "</ArrivalTime>"
requestStr = requestStr & "<DepartTime>" & depHours & depMins & "</DepartTime>"
requestStr = requestStr & "<Location>" & airportRef & "</Location>"
requestStr = requestStr & "<NumberOfPax>" & passengers & "</NumberOfPax>"
requestStr = requestStr & "</Itinerary>"

requestStr = requestStr & "</API_Request>"
response.write(requestStr & "<br><br>")

strxml = "request=" & Server.UrlEncode(requestStr)
Response.Write(strxml & "<br><br>")

dim xmlServerHttp, objXML, xmlloaded

Set xmlServerHttp = Server.CreateObject("Microsoft.XMLHTTP")
xmlServerHttp.Open "POST", " , False
xmlServerHttp.setRequestHeader "Content-Type","application/x-xmlServerHttp.Send strxml
set objXML = Server.CreateObject("Microsoft.XMLDOM")


xmlloaded=objXML.loadXML(xmlServerHttp.responsetext)

Response.Write "<xmp>"
Response.Write(xmlServerHttp.responsetext)
Response.Write "</xmp>"



This produces the following result:


<API_Reply System=”XXX”
Version=”1.0”
Product=”CarPark”
Customer=”A”
Session=”000000003”
RequestCode=”1”
Result=”OK”>
<CarPark c=”1”>
<CarParkCode>LGW8</CarParkCode>
<CarParkName>CPS Personal Parking</CarParkName>
<ValetService>Y</ValetService>
<OnAirport>Y</OnAirport>
<MinPriceDuration>1</MinPriceDuration>
<TotalPrice>81.00</TotalPrice>
<GatePrice>85.00</GatePrice>
<RequestFlags>
<Registration>Y</Registration>
<CarMake>N</CarMake>
<CarModel>N</CarModel>
<CarColour>N</CarColour>
<CarPickupDate>Y</CarDepartDate>
<CarDropoffTime>N</CarArrivalTime>
<CarPickupTime>N</CarDepartTime>
<CreditCard>N</CreditCard>
<ReturnFlight>N</ReturnFlight>
<Terminal>N</Terminal>
</RequestFlags>
</CarPark>
<CarPark c=”2”>
<CarParkCode>LGW5</CarParkCode>
<CarParkName>Flightpath North</CarParkName>
<ValetService>Y</ValetService>
<OnAirport>Y</OnAirport>
<MinPriceDuration>1</MinPriceDuration>
<TotalPrice>52.80</TotalPrice>
<GatePrice>56.80</GatePrice>
<RequestFlags>
<Registration>N</Registration>
<CarMake>N</CarMake>
<CarModel>N</CarModel>
<CarColour>N</CarColour>
<CarPickupDate>Y</CarDepartDate>
<CarDropoffTime>N</CarArrivalTime>
<CarPickupTime>N</CarDepartTime>
<CreditCard>N</CreditCard>
<ReturnFlight>N</ReturnFlight>
<Terminal>N</Terminal>
</RequestFlags>
</CarPark>
<Pricing>
<CCardSurchargePercent>1.5</CCardSurchargePercent>
<CCardSurchargeMin>1.00</CCardSurchargeMin>
<CCardSurchargeMax>3.00</CCardSurchargeMax>
</Pricing>
</API_Reply>


I need to loop through this response and retrieve the values into variables to be used on my ASP page. The results are split into different car parks and I need each separate element for each car park to be returned.


Can anybody help?




Thanks,





PJORDANNA
 
Well you've got it loaded into the XML DOM variable named objXML ... so copy it out into variables from there or, better yet, just use objXML in the rest of your ASP script.
 
Sheco,

How do I copy it out into variables from there? That's my problem. I don't know the syntax.

By the way the number of parents will vary so I am guessing I need a looping structure?



Panini
 
This will help:

Click DOM on the left menu and under that then click Reference. It will tell you everything better than I can explain it.

Also here is a little VBScript file... it doesn't do exactly what you want it to do but it will help get you started.

Code:
set oFSO = CreateObject("Scripting.FileSystemObject")

set oTXT = oFSO.OpenTextFile("C:\[b]Put_Your_File_Path_Here[/b]\TestData.xml")

set objXML = CreateObject("Microsoft.XMLDOM") 

xmlloaded = objXML.loadXML(oTXT.ReadAll) 

if cBool(xmlloaded) then
  msgbox "XML Loaded OK"

  Set RootNode = objXML.documentElement
  Set ChildNodesList = RootNode.ChildNodes

  iCount = 1	
  For Each ThisChild in ChildNodesList
    msgbox ThisChild.xml, , "iCount = " & iCount
    iCount = iCount + 1
  Next
else
  msgbox  objXML.parseError.reason, ,"Error code = " & objXML.parseError.errorCode 
end if


Set ThisChild = Nothing
Set ChildNodesList = Nothing
Set RootNode = Nothing
set objXML = nothing
Set oTXT = Nothing
Set oFSO = Nothing

I copied your XML into a little file to make this script... also there is an error in your XML but I just removed those line to test the script.

The XML problem is 3 lines in a row, the first one is:
<CarPickupDate>Y</CarDepartDate>

See the close tag doesn't match the open tag.
 
Oh, an just to clarify... that thing I wrote is a script file so copy and paste into a text file than change the extentsion to .VBS and double click it to run it.

Also you need to edit it to change where I put Put_Your_File_Path_Here and replace it with the file path to a static copy of your XML file.

Here is the text that I pasted to make the XML file, notice that I just removed the 3 sub nodes that caused the parse error from both park nodes c1 and c2... just for testing the script.

Code:
<API_Reply System="XXX" Version="1.0" Product="CarPark" Customer="A" Session="000000003" RequestCode="1" Result="OK">
<CarPark c="1">
<CarParkCode>LGW8</CarParkCode>
<CarParkName>CPS Personal Parking</CarParkName>
<ValetService>Y</ValetService>
<OnAirport>Y</OnAirport>
<MinPriceDuration>1</MinPriceDuration>
<TotalPrice>81.00</TotalPrice>
<GatePrice>85.00</GatePrice>
<RequestFlags>
<Registration>Y</Registration>
<CarMake>N</CarMake>
<CarModel>N</CarModel>
<CarColour>N</CarColour>
<CreditCard>N</CreditCard>
<ReturnFlight>N</ReturnFlight>
<Terminal>N</Terminal>
</RequestFlags>
</CarPark>
<CarPark c="2">
<CarParkCode>LGW5</CarParkCode>
<CarParkName>Flightpath North</CarParkName>
<ValetService>Y</ValetService>
<OnAirport>Y</OnAirport>
<MinPriceDuration>1</MinPriceDuration>
<TotalPrice>52.80</TotalPrice>
<GatePrice>56.80</GatePrice>
<RequestFlags>
<Registration>N</Registration>
<CarMake>N</CarMake>
<CarModel>N</CarModel>
<CarColour>N</CarColour>
<CreditCard>N</CreditCard>
<ReturnFlight>N</ReturnFlight>
<Terminal>N</Terminal>
</RequestFlags>
</CarPark>
<Pricing>
<CCardSurchargePercent>1.5</CCardSurchargePercent>
<CCardSurchargeMin>1.00</CCardSurchargeMin>
<CCardSurchargeMax>3.00</CCardSurchargeMax>
</Pricing>
</API_Reply>
[code]
 
Baaah, I should have hit the PREVIEW button...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top