Forum Moderators: open

Message Too Old, No Replies

XML , ASP and DOM question Getting the Node I need

Xml, Dom, Asp, Ups

         

DigiMedia

5:55 am on Apr 16, 2003 (gmt 0)

10+ Year Member



Hello All,

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]

duckhunter

1:18 pm on Apr 17, 2003 (gmt 0)

10+ Year Member



DigiMedia,
Looks for one like the XML is malformed. It has TWO ROOT NODES (Response & RatedShipment) when only one root node is allowed. The code below will write out the error if loading fails. I added a ROOT NODE TAG of "UPS" to the XML you show so it is well-formed XML and loads properly. Is this the XML that is coming back from UPS or just part of it?

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

Gizmare

11:27 pm on May 16, 2003 (gmt 0)

10+ Year Member



Oops wrong problem!

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

macrost

6:54 pm on May 17, 2003 (gmt 0)

10+ Year Member



DigiMedia,
I created a solution a while back for another user here. <justlookedatmyoldthread> Have you looked into using straight xsl? If you want, either mattglet or I can help you on using this solution. Feel free to sticky him or me.
Mac

mattglet

2:08 pm on May 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cody-
i have a fully operational UPS rate calculator that is up and running, xml/xsl/asp all wrapped into one. i have also a couple other UPS tools implemented. if you need any help at all, or would like to see an example, feel free to sticky mail me.

-Matt

DigiMedia

2:56 am on May 19, 2003 (gmt 0)

10+ Year Member




Thanks for all of the info! I have since figured out the code for this to work, but if I have any further questions I will definately know where to come.

Thanks Guys,
Cody
DigiMedia Design