Forum Moderators: open
I have am an ASP guy that has been thrown into the world of XML by a project that I am working on. I am currently trying to implement the UPS online calculation into my shopping system.
I am currently stuck trying to extract the info that I need from the response XML that UPS sends back to my system.I was hoping that someone may have some info that would help out. I have posted a portion of the XML that is returned by UPS and the code in question. Idealy the select menu would look something like this:
<select name=shipping>
<option>
(*XML RatedShipment/Service/Code)
(*XML RatedShipment/TotalCharges/MonetaryValue)
</option>
</select>
Here is the code that I have so far. It currently Prints out all of the Elements in each node in the option tags. I just need to figure out how to get it to pull out the correct two elements that I need.
*********************
ASP Code
*********************
'Load The Response XML into XML DOM
Dim mydoc, responseXML
responseXML = xmlhttp.responseText
Set mydoc=Server.CreateObject("Microsoft.XMLDOM")
mydoc.loadXML(responseXML)
'Create a select table from the response XML
response.Write("<select name='shipping'>")
'Create A Nodelist of All The RatedShipments
Set NodeList = mydoc.documentElement.selectNodes("RatedShipment")
For Each x In NodeList
'Service/Code
'TotalCharges/MonetaryValue
response.write("<option>")
response.write(x.Text)
response.write("</option>")
Next
response.Write("</select>")
*********************
//ASP Code
*********************
*********************
Here is the XML That I Am Working From
*********************
<?xml version="1.0" encoding="iso-8859-1"?>
<Response>
<TransactionReference>
<CustomerContext>Rating and Service</CustomerContext>
<XpciVersion>1.0001</XpciVersion>
</TransactionReference>
<ResponseStatusCode>1</ResponseStatusCode>
<ResponseStatusDescription>Success</ResponseStatusDescription>
</Response>
<RatedShipment>
<Service>
<Code>12</Code>
</Service>
<BillingWeight>
<UnitOfMeasurement>
<Code>LBS</Code>
</UnitOfMeasurement>
<Weight>1.0</Weight>
</BillingWeight>
<TransportationCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>7.04</MonetaryValue>
</TransportationCharges>
<ServiceOptionsCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>0.00</MonetaryValue>
</ServiceOptionsCharges>
<TotalCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>7.04</MonetaryValue>
</TotalCharges>
<GuaranteedDaysToDelivery>3</GuaranteedDaysToDelivery>
<ScheduledDeliveryTime/>
<RatedPackage>
<TransportationCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>7.04</MonetaryValue>
</TransportationCharges>
<ServiceOptionsCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>0.00</MonetaryValue>
</ServiceOptionsCharges>
<TotalCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>7.04</MonetaryValue>
</TotalCharges>
<Weight>1.0</Weight>
<BillingWeight>
<UnitOfMeasurement>
<Code>LBS</Code>
</UnitOfMeasurement>
<Weight>1.0</Weight>
</BillingWeight>
</RatedPackage>
</RatedShipment>
<RatedShipment>
<Service>
<Code>59</Code>
</Service>
<BillingWeight>
<UnitOfMeasurement>
<Code>LBS</Code>
</UnitOfMeasurement>
<Weight>1.0</Weight>
</BillingWeight>
<TransportationCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>10.91</MonetaryValue>
</TransportationCharges>
<ServiceOptionsCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>0.00</MonetaryValue>
</ServiceOptionsCharges>
<TotalCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>10.91</MonetaryValue>
</TotalCharges>
<GuaranteedDaysToDelivery>2</GuaranteedDaysToDelivery>
<ScheduledDeliveryTime>12:00 Noon</ScheduledDeliveryTime>
<RatedPackage>
<TransportationCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>10.91</MonetaryValue>
</TransportationCharges>
<ServiceOptionsCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>0.00</MonetaryValue>
</ServiceOptionsCharges>
<TotalCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>10.91</MonetaryValue>
</TotalCharges>
<Weight>1.0</Weight>
<BillingWeight>
<UnitOfMeasurement>
<Code>LBS</Code>
</UnitOfMeasurement>
<Weight>1.0</Weight>
</BillingWeight>
</RatedPackage>
</RatedShipment>
*********************
//XML
*********************
Thanks Guys!
Cody Hindman
<no sigs>
[edited by: Xoc at 7:34 pm (utc) on April 16, 2003]
[edit reason] Removed sig [/edit]
Once loaded, loop for 0 to the (length -1) of the NodeList (length is 2 in your case so we loop through the 0 and 1 ordinals) using the selectSingleNode for each of your child values you want.
Dim mydoc, NodeList
Set mydoc = CreateObject("Microsoft.XMLDOM")
If mydoc.loadXML(responseXML) Then
'Successfully Loaded XML
Set NodeList = mydoc.selectNodes("//UPS/RatedShipment")
response.Write("<select name='shipping'>")
For x = 0 To NodeList.length - 1
response.write("<option>")
Response.Write NodeList.Item(x).selectSingleNode("Service/Code").Text & " - " & NodeList.Item(x).selectSingleNode("TotalCharges/MonetaryValue").Text
response.write("</option>")
Next
response.Write("</select>")
Else
'Failed to Load XML
Response.Write "Did Not Load Response XML: " & mydoc.parseError.reason
End If
Set mydoc = Nothing
Set NodeList = Nothing
Here is how I parse it:
Set ElemCount = objXML.getElementsByTagName("RatedShipment")
Set ElemList = objXML.getElementsByTagName("RatedShipment/TotalCharges")
Set ElemList2 = objXML.getElementsByTagName("RatedShipment/Service/Code")
For i=0 To (ElemCount.length -1)
Select Case ElemList2.item(i).Text
'Case "01"
'
Case "02"
if Session("ShipHow") = "02" Then
Response.Write("<option selected value=""" & ElemList2.item(i).Text & """>")
Else
Response.Write("<option value=""" & ElemList2.item(i).Text & """>")
End If
Response.Write("UPS 2nd Day Air - $" & Replace(ElemList.item(i).Text, "USD ",""))
Response.Write("</option>")
dShipMethod = "02"
d2ndDayAirPrice = Replace(ElemList.item(i).Text, "USD ","")
Case "03"
if Session("ShipHow") = "03" Then
Response.Write("<option selected value=""" & ElemList2.item(i).Text & """>")
Else
Response.Write("<option value=""" & ElemList2.item(i).Text & """>")
End If
Response.Write("UPS Ground - $" & Replace(ElemList.item(i).Text, "USD ",""))
Response.Write("</option>")
dShipMethod = "03"
dGroundPrice = Replace(ElemList.item(i).Text, "USD ","")
Case "12"
if Session("ShipHow") = "12" Then
Response.Write("<option selected value=""" & ElemList2.item(i).Text & """>")
Else
Response.Write("<option value=""" & ElemList2.item(i).Text & """>")
End If
Response.Write("UPS 3 Day Select - $" & Replace(ElemList.item(i).Text, "USD ",""))
Response.Write("</option>")
dShipMethod = "12"
d3DayPrice = Replace(ElemList.item(i).Text, "USD ","")
End Select
sString = sString & ElemList2.item(i).Text & "," & Replace(ElemList.item(i).Text, "USD ","") & "<br>"
Next