我已经编写了一个代码来读取经典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
%>
此代码未提供正确的输出。 请帮帮我。
xml是:
<Product>
<ProductCode>abc</ProductCode>
<ProductName>CC Skye Hinge Bracelet Cuff with Buckle in Black</ProductName>
</Product>
答案 0 :(得分: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
%>
注意:
Server.MapPath
解析虚拟路径selectNodes
和selectSingleNode
代替getElementsByTagName
。 getElementsByTagName
扫描所有后代,因此可以返回意外结果,然后即使您知道只需要一个返回值,也总是需要索引结果。Server.HTMLEncode
数据。答案 1 :(得分:4)
这里有一个如何读取数据的例子,给定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>
以下脚本
<%
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
%>
给出
abc CC Skye Hinge Bracelet Cuff with Buckle in Black
dfg another product