开发者

load xml data from external site the pasre data and then send response back in xml to browser

开发者 https://www.devze.com 2023-04-03 11:11 出处:网络
I need to request data from an API parse specific data from it and return the response in xml. What I have so far is this which works fine but only returns text rather than xml

I need to request data from an API parse specific data from it and return the response in xml.

What I have so far is this which works fine but only returns text rather than xml

<%
dim objXMLL
Dim objXML, objXSL, x

Set objXML = CreateObject("MSXML2.DOMDocument")
objXML.async = False
objXML.setProperty "ServerHTTPRequest", True
objXML.Load "http://www.theapisite.com/net/WebService.aspx?Login=name@thesite.com&EncryptedPassword=2873287372372326372638374837473473674634763784637843648736&EDI_Name=Generic\Products&SELECT_Columns=p.ProductCode,pe.Availability&WHERE_Column=p.ProductCode&WHERE_Value=1430-09"
objXML.setProperty "SelectionLanguage", "XPath"

For Each x In objXML.documentElement.selectNodes(".//Products")
Response.write x.nodename & " = " & x.Text 
Next
%>

Here is the XML returned from the API.

<?xml version="1.0" encoding="UTF-8"?>
    <xmldata>
         <Products>
              <ProductCode>1430-09</ProductCode>
              <ProductID>37717</ProductID>
              <Availability>Out of Stock</Availability>
         </Products>
    </xmldata>

This is what I want to send back to the browser as the response instead of the text. How do I only send the XML I need and return it in the response with the names as shown.

<?xml version="1.0" encoding="UTF-8"?>
    <data>
         <Products>
         开发者_开发问答     <Product>1430-09</Product>
              <ID>37717</ID>
         </Products>
    </data>


If I understand you correctly, you want transform your http request response XML to another schema and write that to the browser? If this the case, you can use an XSL Transform. Or you can programmatically create a new xml document in your ASP and copy the data over as appropriate

Dim xmldoc: set xmldoc = CreateObject("MSXML2.DomDocument")
xmldoc.async = false
' add the xml processing instruction
Dim instruction
Set instruction = xmldoc.createProcessingInstruction("xml", "version=""1.0"" encoding=""UTF-8""")
xmldoc.appendChild instruction 
' create the root nodes
Dim data: set data = xmldoc.createElement("data")
xmldoc.appendChild data
Dim products: set products = xmldoc.createElement("Products")
data.appendChild products

Dim x
For Each x In objXML.documentElement.selectNodes(".//Products")
     ' get each child node
     Dim productcode: Set productcode = x.selectSingleNode("ProductCode")
     Dim productid: Set productid = x.selectSingleNode("ProductID")
     ' pass over the data from old node to new
     Dim product: Set product = xmldoc.createElement("Product")
     product.text = productcode.text
     products.appendChild product
     Dim id: set id = xmldoc.createElement("ID")
     id.text = productid.text
     products.appendChild id
Next
' write the final xml to the response stream
Response.Write xmldoc.xml

Depending on the size of the input XML, you may want to compare the performance between this and using an XSL transform

0

精彩评论

暂无评论...
验证码 换一张
取 消