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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML & VBScript 1

Status
Not open for further replies.

kizmar2

Programmer
May 25, 2004
164
US
I'm trying to talk to UPS's XML Online Rate & Services Tool via an HTTP XML post, but having issues. Based on their documentation they want two requests sent in one XML document, but the DOM doesn't allow that... so I'm now sure what to do.

Here's an example of what I wrote to test this:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<%@ Language=VBScript %>

<html>
<head>
	<title>Outdoor Digs</title>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<%

'-----------------------------------------------------------------------
' Define Variables
'-----------------------------------------------------------------------
Dim xmlDoc, XMLhttp, xmlDoc2, Response_Doc, Test_Doc

' Put together some XML to post off
' Send account login stuff
xmlString = 			"<?xml version=""1.0"" encoding=""UTF-8""?>" & _
						"<AccessRequest xml:lang=""en-US"">" & _
						"	<AccessLicenseNumber>2BEC0878849792C4</AccessLicenseNumber>" & _
						"	<UserId>Outdoordigs</UserId>" & _
						"	<Password>Trilogy</Password>" & _
						"</AccessRequest>"
' Set up request type
xmlString = xmlString & "<?xml version=""1.0"" encoding=""UTF-8""?>" & _
						"<RatingServiceSelectionRequest xml:lang=""en-US"">" & _
						"	<Request>" & _
						"		<TransactionReference>" & _
						"			<CustomerContext>Bare Bones Rate Request</CustomerContext>" & _
						"			<XpciVersion>1.0001</XpciVersion>" & _
						"		</TransactionReference>" & _
						"		<RequestAction>Rate</RequestAction>" & _
						"		<RequestOption>Rate</RequestOption>" & _
						"	</Request>"
' Some code thing...
xmlString = xmlString & "	<PickupType>" & _
						"		<Code>01</Code>" & _
						"	</PickupType>"
' Location of person having item shipped
xmlString = xmlString & "	<Shipment>" & _
						"		<Shipper>" & _
						"			<Address>" & _
						"				<PostalCode>44129</PostalCode>" & _
						"				<CountryCode>US</CountryCode>" & _
						"			</Address>" & _
						"		</Shipper>"
' Ship to address info
xmlString = xmlString & "		<ShipTo>" & _
						"			<Address>" & _
						"				<PostalCode>44129</PostalCode>" & _
						"				<CountryCode>US</CountryCode>" & _
						"			</Address>" & _
						"		</ShipTo>"
' Ship from address info
xmlString = xmlString & "		<ShipFrom>" & _
						"			<Address>" & _
						"				<PostalCode>32779</PostalCode>" & _
						"				<CountryCode>US</CountryCode>" & _
						"			</Address>" & _
						"		</ShipFrom>"
' Another service code thing...
xmlString = xmlString & "		<Service>" & _
						"			<Code>01</Code>" & _
						"		</Service>"
' Package info
xmlString = xmlString & "		<Package>" & _
						"			<PackagingType>" & _
						"				<Code>02</Code>" & _
						"			</PackagingType>"
' Package dimensions
xmlString = xmlString & "			<Dimensions>" & _
						"				<UnitOfMeasurement>" & _
						"					<Code>IN</Code>" & _
						"				</UnitOfMeasurement>" & _
						"				<Length>20</Length>" & _
						"				<Width>20</Width>" & _
						"				<Height>20</Height>" & _
						"			</Dimensions>"
' Package weight
xmlString = xmlString & "			<PackageWeight>" & _
						"				<UnitOfMeasurement>" & _
						"					<Code>LBS</Code>" & _
						"				</UnitOfMeasurement>" & _
						"				<Weight>23</Weight>" & _
						"			</PackageWeight>" & _
						"		</Package>" & _
						"	</Shipment>" & _
						"</RatingServiceSelectionRequest>"

'-----------------------------------------------------------------------
' Open XML document
'-----------------------------------------------------------------------
Set xmlDoc = CreateObject("Microsoft.XMLDOM") 'CreateObject("MSXML2.DOMDocument")
	xmlDoc.SetProperty "ServerHTTPRequest", False
	xmlDoc.async = False
	xmlDoc.loadXML(xmlString)
	
	'Set Root = xmlDoc.documentElement 
	'response.Write(Root.xml)

	' Check for errors
	Response.Write "<br>"
	Response.Write "<b>Checking for errors when loading XML:</b><br>"
	Response.Write "-----------------------------------------<br>"
	Response.Write "<b>Error Code:</b> " & xmlDoc.ParseError & "<br>"
	Response.Write "<b>Error Description:</b> " & xmlDoc.ParseError.reason & "<br>"
	Response.Write "<b>Error File Position:</b> " & xmlDoc.ParseError.filepos & "<br>"
	Response.Write "<b>Error Line:</b> " & xmlDoc.ParseError.line & "<br>"
	Response.Write "<b>Error Line Position:</b> " & xmlDoc.ParseError.linepos & "<br>"
	Response.Write "<b>Error Source Text:</b> " & xmlDoc.ParseError.srcText & "<br>"
	Response.Write "<br>"
	
	'-----------------------------------------------------------------------
	' Output the XML that I just put into this file
	'-----------------------------------------------------------------------
	Response.Write "<br>"
	Response.Write "<b>My XML:</b><br>"
	Response.Write "-----------------------------------------<br>"
	Set Root = xmlDoc.documentElement 
	response.Write(Root.xml)
	Response.Write Response_Doc & "<br>"

'-----------------------------------------------------------------------
' Send XML request
'-----------------------------------------------------------------------
' Testing site URL: [URL unfurl="true"]https://wwwcie.ups.com/ups.app/xml/Rate[/URL]
' Live site URL: [URL unfurl="true"]https://www.ups.com/ups.app/xml/Rate[/URL]
Set XMLhttp = CreateObject("MSXML2.ServerXMLHTTP")
	XMLhttp.Open "POST", "[URL unfurl="true"]https://wwwcie.ups.com/ups.app/xml/Rate",[/URL] False
	XMLhttp.Send xmlDoc.xml

'-----------------------------------------------------------------------
' Get server status
'-----------------------------------------------------------------------
Response.Write "<br>"
Response.Write "<b>xmlSvr Server Status:</b><br>"
Response.Write "-----------------------------------------<br>"
Response.Write "<b>Status (Value must be 200): </b>" & XMLhttp.status & "<br>"
Response.Write "<b>ReadyState (Value must be 4): </b>" & XMLhttp.ReadyState & "<br>"
Response.Write "<b>StatusText (Value must be OK): </b>" & XMLhttp.StatusText & "<br>"
Response.Write "<b>AllResponseHeaders:</b><br>" & XMLhttp.GetAllResponseHeaders & "<br>"

'-----------------------------------------------------------------------
' Get XML response from xmlSvr
'-----------------------------------------------------------------------
Set xmlDoc2 = CreateObject("MSXML2.DOMDocument")
xmlDoc2.setProperty "ServerHTTPRequest", True
xmlDoc2.async = False
xmlDoc2.LoadXML XMLhttp.ResponseXML.xml
Response.Write "<br>"
Response.Write "<b>UPS XML Response:</b><br>"
Response.Write "-----------------------------------------<br>"
Response_Doc = xmlhttp.responseXML.xml
Response_Doc = Replace (Response_Doc,"<","&lt;")
Response_Doc = Replace (Response_Doc,">","&gt;")
Response.Write Response_Doc & "<br>"

%>

</body>
</html>

Here's the URL where this page sits:
I'm working on this right now so that link may not always display the same stuff.

KizMar
------------
 
[1]>Based on their documentation they want two requests sent in one XML document

It should only be understood as two consecutive "requests" each in the format of a well-formed xml document, making it a sort of xml document fragment.

[2]> but the DOM doesn't allow that...

Only in the sense that you use loadxml trying to parse the complete string.

[3]>so I'm now[sic] sure what to do.

You have two options.

[3.1] Bypassing the section where you try to test for well-formed-ness and post the xmlString directly. This, you have to make sure you build the xmlString correctly without typos etc.

[3.2] Break the xmlString into two part xmlString1 and xmlString2, each being parse for well-formed-ness.

[3.2.1] In any case, your parse for well-formed-ness does not serve its purpose to the letter. If the parseerror return nonzero errorcode, you still post the info... hence, it is not logic to do that. The purpose is to stop posting until you finally make out correctly the two requests.

[4] When you retrieve the response, there is some unnecessary detour from responseXML to xmlDoc2 etc. Why not simply get the
> Response_Doc = xmlhttp.responseXML.xml
[tt] Response_Doc = xmlhttp.responseText[/tt]
and comment out all the setup of xmlDoc2. It can serve a purpose, but not the purpose as shown in your script.

The script for [3.1] option and with consideration of [4] would look like this.
[tt]
<%
[COLOR=green yellow]'etc etc[/color]
xmlString = xmlString & " <PackageWeight>" & _
" <UnitOfMeasurement>" & _
" <Code>LBS</Code>" & _
" </UnitOfMeasurement>" & _
" <Weight>23</Weight>" & _
" </PackageWeight>" & _
" </Package>" & _
" </Shipment>" & _
"</RatingServiceSelectionRequest>"
[blue]'completely take out the part ("Open XML document") for parsing it with xmlDoc.[/blue]
'-----------------------------------------------------------------------
' Send XML request
'-----------------------------------------------------------------------
' Testing site URL: ' Live site URL: Set XMLhttp = CreateObject("MSXML2.ServerXMLHTTP")
XMLhttp.Open "POST", " False
XMLhttp.Send [red]xmlString[/red]
[COLOR=green yellow]'etc etc[/color]
'-----------------------------------------------------------------------
' Get XML response from xmlSvr
'-----------------------------------------------------------------------
[red]'[/red]Set xmlDoc2 = CreateObject("MSXML2.DOMDocument")
[red]'[/red]xmlDoc2.setProperty "ServerHTTPRequest", True
[red]'[/red]xmlDoc2.async = False
[red]'[/red]xmlDoc2.LoadXML XMLhttp.ResponseXML.xml
Response.Write "<br>"
Response.Write "<b>UPS XML Response:</b><br>"
Response.Write "-----------------------------------------<br>"
Response_Doc = xmlhttp.[red]responseText[/red]
Response_Doc = Replace (Response_Doc,"<","&lt;")
Response_Doc = Replace (Response_Doc,">","&gt;")
Response.Write Response_Doc & "<br>"

%>
[/tt]
There may be fine detail to make good, but the big layout is like the above.
 
Awesome!

Using the string instead of the document worked! I didn't even think about the fact that after I checked the XML itself, that it was killing the variable.

Thank you very much for the input!

KizMar
------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top