1

I am trying to request xml data from a e-commerce API in ASP classic and "transform" the data into new XML and return that data as the response.

This is the ASP script.

<%

product_code = Request.QueryString("product_code")
url = "http://www.the site.com/net/WebService.aspx?Login=name@thehost.com&EncryptedPassword=***********&EDI_Name=Generic\Products&SELECT_Columns=p.ProductCode,p.ProductName,pd.ProductDescriptionShort,pe.ListPrice,pe.ProductPrice,pe.SalePrice&WHERE_Column=p.ProductCode&WHERE_Value=" & product_code
Set xData = CreateObject("Microsoft.XMLHTTP")
xData.Open "get", url, False
xData.Send 
Response.ContentType = "text/xml"
Response.write (xData.responseText)
Set xData = Nothing

%>

Here is a sample of the returned data from the ASP script which calls the API. For instance ifthe above page is called getdata.asp and I call it with www.thesite.com/getdata.asp?product_code=m406789 then the following will be returned.

<?xml version="1.0" encoding="UTF-8"?>
<xmldata>
   <Products>
       <ProductCode>M406789</ProductCode>
       <ProductID>858</ProductID>
       <ProductName>M406789 Ignition Box</ProductName>
       <ProductDescriptionShort>&lt;img alt="" src="/v/vspfiles/assets/images/alliance_small.jpg" align="right" /&gt;Ignition Box</ProductDescriptionShort>
       <ListPrice>134.2200</ListPrice>
       <ProductPrice>80.5300</ProductPrice>
       <SalePrice>59.9500</SalePrice>
   </Products>
</xmldata>

What I would like to do is have the returned XML data look like this.

<?xml version="1.0" encoding="UTF-8"?>
<hotspot>
    <ProductCode>M406789</ProductCode>
    <ProductID>858</ProductID>
    <ProductName>M406789 Ignition Box</ProductName>
    <ProductDescriptionShort>&lt;img alt="" src="/v/vspfiles/assets/images/alliance_small.jpg" align="right" /&gt;Ignition Box</ProductDescriptionShort>
    <ListPrice>134.2200</ListPrice>
    <ProductPrice>80.5300</ProductPrice>
    <SalePrice>59.9500</SalePrice>
</hotspot>

Any help or sample code would be much appreciated. Not sure what route to take here.

user357034
  • 10,731
  • 19
  • 58
  • 72

2 Answers2

1

Could be like this:

<%
product_code = Request.QueryString("product_code")
url = "http://www.the site.com/net/WebService.aspx?Login=name@thehost.com&EncryptedPassword=***********&EDI_Name=Generic\Products&SELECT_Columns=p.ProductCode,p.ProductName,pd.ProductDescriptionShort,pe.ListPrice,pe.ProductPrice,pe.SalePrice&WHERE_Column=p.ProductCode&WHERE_Value=" & product_code
Set xData = Server.CreateObject("MSXML2.ServerXMLHTTP")
    xData.Open "GET", url, False
    xData.Send
Dim xNewDoc
Set xNewDoc = xData.responseXML 'ResponseXml returns DOMDocument object
    With xNewDoc
        .RemoveChild .FirstChild
        .InsertBefore .createProcessingInstruction("xml","version='1.0' encoding='ISO-8859-1'"), .FirstChild
        Set hotspot = .CreateElement("hotspot")
        For Each e In .SelectSingleNode("//Products").ChildNodes
            hotspot.AppendChild e
        Next
        Set .DocumentElement = hotspot
        Response.ContentType = "text/xml"
        Response.Write .Xml
    End With
Set xNewDoc = Nothing
Set xData = Nothing
%>
Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
  • Well this code kinda works but only if I use this Set xData = Server.CreateObject("Microsoft.XMLHTTP") instead of Set xData = Server.CreateObject("MSXML2.ServerXMLHTTP") Any ideas why? – user357034 Oct 09 '11 at 21:45
  • 1
    Why Server.CreateObject? Have a look from [What's the Difference between WScript.CreateObject, Server.CreateObject and CreateObject?](http://blogs.msdn.com/b/ericlippert/archive/2004/06/01/145686.aspx) Difference ServerXMLHTTP and XMLHTTP : http://stackoverflow.com/questions/1163045/differences-between-msxml2-serverxmlhttp-and-winhttp-winhttprequest (First Quote) – Kul-Tigin Oct 10 '11 at 12:11
  • Actually that was not my question but rather I stated that you code above worked great with the exception of this line of code Set xData = Server.CreateObject("MSXML2.ServerXMLHTTP"). When I used this it would fail. I replaced that with this Set xData = CreateObject("Microsoft.XMLHTTP") and it worked fine. Any idea why your line of code not work for me? – user357034 Oct 10 '11 at 12:44
  • 1
    Probably it's "cannot create object", may be related to permissions. http://forums.iis.net/t/1164511.aspx – Kul-Tigin Oct 10 '11 at 13:10
  • I would guess so but I do not have any access to the server to change/fix it. – user357034 Oct 10 '11 at 13:12
  • Reporting to service provider / sysadmin (or who has access) is a good idea in such case. Or you use worked code, your decision :) – Kul-Tigin Oct 10 '11 at 13:28
  • Getting them to make any changes, sadly is not a option. :( – user357034 Oct 10 '11 at 13:30
1

Kul has the answer (well just the code that probably will work) but a lot has been left unsaid that should be said.

Well first of all .... Yikes!!!! :

 url = "http://www.the site.com/net/WebService.aspx?Login=name@thehost.com&EncryptedPassword=***********&EDI_Name=Generic\Products&SELECT_Columns=p.ProductCode,p.ProductName,pd.ProductDescriptionShort,pe.ListPrice,pe.ProductPrice,pe.SalePrice&WHERE_Column=p.ProductCode&WHERE_Value=" & product_code

Drag the above scroll bar thumb to right to reveal the security managers nightmare. Clear SQL code embedded in a URL string! It seems to me to be quite possible to muck about with that API to inject nasty SQL exection, my guess would be that behind those querystrings is a simple string contatenation. If so the API should be withdrawn from the website with immediate effect.

Having got that off my chest. Use ServerXMLHTTP to talk to another server, XMLHTTP is not thread safe and should not be used in ASP code.

Also why ISO-8859-1? Just leave it at UTF-8.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • That is the API we have to use, no control over that :( As for the ISO-8859-1 it isn't necessary that we change it from UTF-8. Also could you explain "thread safe" in this context. Like if you were talking to a two year old , please. :) THX – user357034 Oct 09 '11 at 21:32