我需要从API请求数据解析来自它的特定数据并以xml返回响应。
到目前为止,我的工作正常,但只返回文本而不是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
%>
以下是从API返回的XML。
<?xml version="1.0" encoding="UTF-8"?>
<xmldata>
<Products>
<ProductCode>1430-09</ProductCode>
<ProductID>37717</ProductID>
<Availability>Out of Stock</Availability>
</Products>
</xmldata>
这是我想要作为响应而不是文本发送回浏览器的内容。我如何只发送我需要的XML并将其返回到响应中,并显示如下所示的名称。
<?xml version="1.0" encoding="UTF-8"?>
<data>
<Products>
<Product>1430-09</Product>
<ID>37717</ID>
</Products>
</data>
答案 0 :(得分:1)
如果我理解正确,您希望将您的http请求响应XML转换为另一个架构并将其写入浏览器吗?如果是这种情况,您可以使用XSL Transform。或者,您可以在ASP中以编程方式创建新的xml文档,并根据需要复制数据
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
根据输入XML的大小,您可能希望比较它与使用XSL转换之间的性能