本文介绍了使用经典 ASP 读取 xml 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了一段经典asp中读取xml数据的代码如下:
I have written a code for reading xml data in classic asp as follows:
<%
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
%>
此代码没有给出正确的输出.请帮帮我.
This code is not giving proper output.Please help me out.
xml 是:
<Product>
<ProductCode>abc</ProductCode>
<ProductName>CC Skye Hinge Bracelet Cuff with Buckle in Black</ProductName>
</Product>
推荐答案
试试这个:
<%
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
%>
注意事项:
- 不要使用 Microsoft.XMLDOM 使用显式 MSXML2.DOMDocument.3.0
- 使用
Server.MapPath
解析虚拟路径 - 使用
selectNodes
和selectSingleNode
代替getElementsByTagName
.getElementsByTagName
扫描所有后代,因此可能会返回意外结果,然后您始终需要对结果进行索引,即使您知道您只需要一个返回值. - 发送到响应时总是
Server.HTMLEncode
数据. - 不要把 ( ) 放在奇怪的地方,这是 VBScript 而不是 JScript.
- Don't use Microsoft.XMLDOM use the explicit MSXML2.DOMDocument.3.0
- Use
Server.MapPath
to resolve virtual paths - Use
selectNodes
andselectSingleNode
instead ofgetElementsByTagName
. ThegetElementsByTagName
scans all descendants so can return unexpected results and then you always need to index into the results even though you know you expect only one return value. - Always
Server.HTMLEncode
data when sending to the response. - Don't put ( ) in weird places, this is VBScript not JScript.
这篇关于使用经典 ASP 读取 xml 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!