xml 使用经典ASP读取xml数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11522316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:35:22  来源:igfitidea点击:

Reading xml data using classic ASP

xmlasp-classic

提问by Rash

I have written a code for reading xml data in classic asp as follows:

我写了一段经典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 
%>

This code is not giving proper output. Please help me out.

这段代码没有给出正确的输出。请帮帮我。

The xml is:

xml是:

<Product>
   <ProductCode>abc</ProductCode>
   <ProductName>CC Skye Hinge Bracelet Cuff with Buckle in Black</ProductName>
</Product>

回答by AnthonyWJones

Try this:

尝试这个:

<%   

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   

%> 

Notes:

笔记:

  • Don't use Microsoft.XMLDOM use the explicit MSXML2.DOMDocument.3.0
  • Use Server.MapPathto resolve virtual paths
  • Use selectNodesand selectSingleNodeinstead ofgetElementsByTagName. The getElementsByTagNamescans 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.HTMLEncodedata when sending to the response.
  • Don't put ( ) in weird places, this is VBScript not JScript.
  • 不要使用 Microsoft.XMLDOM 使用显式 MSXML2.DOMDocument.3.0
  • 使用Server.MapPath来解决虚拟路径
  • 使用selectNodesselectSingleNode代替getElementsByTagName。该getElementsByTagName扫描所有后代等都可以返回意外的结果,然后你总是需要索引的结果,即使你知道你希望只有一个返回值。
  • Server.HTMLEncode发送到响应时总是数据。
  • 不要把 ( ) 放在奇怪的地方,这是 VBScript 而不是 JScript。

回答by peter

Here an example how to read your data, given the xml is

这是一个如何读取数据的示例,假设 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>

The following script

下面的脚本

<%

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

%>

gives

abc CC Skye Hinge Bracelet Cuff with Buckle in Black
dfg another product