xml - 使用经典 ASP 读取 xml 数据

标签 xml asp-classic

我在经典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>

最佳答案

试试这个:

<%   

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解析虚拟路径
  • 使用 selectNodesselectSingleNode 而不是 getElementsByTagNamegetElementsByTagName 扫描所有后代,因此可以返回意外结果,然后您始终需要对结果进行索引,即使您知道您只需要一个返回值。
  • 发送到响应时总是Server.HTMLEncode数据。
  • 不要把 ( ) 放在奇怪的地方,这是 VBScript 而不是 JScript。

关于xml - 使用经典 ASP 读取 xml 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11522316/

相关文章:

xml - 在哪里可以找到或多或少完整的 Skype 支持的 XML 标记列表?

asp-classic - 如何显示 100x75 的图像,然后单击以原始尺寸显示它们。 ASP经典

java - ASP 经典的 Base64 图像解码器

asp-classic - ASP vb编译错误800A03EA

java - Java 上的 MQ 入门 : Where to begin?

java - 为什么在文件包含特殊 UTF-8 字符时使用 InputSource 修复 SAX 解析器

c# - 在 C# 中将字符串值格式化为 $xx.xx 格式

xml - XSLT1.0 检查是否存在其祖先没有特定属性的后代,但如果有,则其值必须位于 RTF 变量中

arrays - 如何在vbscript中实现可变大小的数组

javascript - 防止 ASP 文本框验证上的超链接