我编写了一个代码来读取经典asp中的xml数据,如下所示:
<%
Dim objxml
Set objxml = Server.CreateObject("Microsoft.XMLDOM")
objxml.async = False
objxml.load ("/abc.in/xml.xml")
set ElemProperty = objxml.getElementsByTagName("Product")
set ElemEN = objxml.getElementsByTagName("Product/ProductCode")
set Elemtown = objxml.getElementsByTagName("Product/ProductName")
set Elemprovince = objxml.getElementsByTagName("Product/ProductPrice")
Response.Write(ElemProperty)
Response.Write(ElemEN)
Response.Write(Elemprovince)
For i=0 To (ElemProperty.length -1)
Response.Write " ProductCode = "
Response.Write(ElemEN)
Response.Write " ProductName = "
Response.Write(Elemtown) & "<br>"
Response.Write " ProductPrice = "
Response.Write(Elemprovince) & "<br>"
next
Set objxml = Nothing
%>
Run Code Online (Sandbox Code Playgroud)
此代码未提供正确的输出.请帮帮我.
xml是:
<Product>
<ProductCode>abc</ProductCode>
<ProductName>CC Skye Hinge Bracelet Cuff with Buckle in Black</ProductName>
</Product>
Run Code Online (Sandbox Code Playgroud)
Ant*_*nes 10
试试这个:
<%
Set objXMLDoc = Server.CreateObject("MSXML2.DOMDocument.3.0")
objXMLDoc.async = False
objXMLDoc.load Server.MapPath("/abc.in/xml.xml")
Dim xmlProduct
For Each xmlProduct In objXMLDoc.documentElement.selectNodes("Product")
Dim productCode : productCode = xmlProduct.selectSingleNode("ProductCode").text
Dim productName : productName = xmlProduct.selectSingleNode("ProductName").text
Response.Write Server.HTMLEncode(productCode) & " "
Response.Write Server.HTMLEncode(productName) & "<br>"
Next
%>
Run Code Online (Sandbox Code Playgroud)
笔记:
Server.MapPath来解决虚拟路径selectNodes而selectSingleNode不是getElementsByTagName.该getElementsByTagName扫描所有后代等都可以返回意外的结果,然后你总是需要索引的结果,即使你知道你希望只有一个返回值.Server.HTMLEncode发送到响应时始终是数据.这是一个如何读取数据的示例,给定的 xml 是
<Products>
<Product>
<ProductCode>abc</ProductCode>
<ProductName>CC Skye Hinge Bracelet Cuff with Buckle in Black</ProductName>
</Product>
<Product>
<ProductCode>dfg</ProductCode>
<ProductName>another product</ProductName></Product>
</Products>
Run Code Online (Sandbox Code Playgroud)
以下脚本
<%
Set objXMLDoc = Server.CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("xml.xml")
Set Root = objXMLDoc.documentElement
Set NodeList = Root.getElementsByTagName("Product")
For i = 0 to NodeList.length -1
Set ProductCode = objXMLDoc.getElementsByTagName("ProductCode")(i)
Set ProductName = objXMLDoc.getElementsByTagName("ProductName")(i)
Response.Write ProductCode.text & " " & ProductName.text & "<br>"
Next
Set objXMLDoc = Nothing
%>
Run Code Online (Sandbox Code Playgroud)
给出
abc CC Skye Hinge Bracelet Cuff with Buckle in Black
dfg another product
Run Code Online (Sandbox Code Playgroud)