使用经典ASP读取XML数据

7

我已经编写了用于在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>

你能提供一些XML样例吗? - thirtydot
<Product> <ProductCode>abc</ProductCode> <ProductName>CC Skye黑色带扣铰链手镯袖口</ProductName> </Product> - Rash
将XML添加到问题中,因为评论不是很适合它。希望能看到更多相关内容。 - AnthonyWJones
2个回答

12

尝试这样做:

<%   

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代替getElementsByTagName。因为getElementsByTagName会扫描所有后代,所以可能返回意外结果,即使你知道只会有一个返回值也需要索引到结果中。
  • 在发送响应时,始终Server.HTMLEncode数据。
  • 不要将()放在奇怪的位置,这是VBScript而不是JScript。

1
MSXML2.DOMDocument.6.0 调用最新版本。 - John

5

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

这个可以工作,但是使用 getElementByTagName 应该引起警惕,因为最终会导致痛苦。 - AnthonyWJones
дёҚпјҢдҪҝз”ЁgetElementByTagNameиҝҷз§Қж–№ејҸжқҘжӯЈзЎ®иҺ·еҸ–е…ғзҙ жҳҜдёҖ件йқһеёёз—ӣиӢҰзҡ„дәӢжғ…гҖӮжңҖеҘҪдҪҝз”ЁselectNodesе’ҢselectSingleNodeжқҘйҒҚеҺҶXMLгҖӮ - AnthonyWJones

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接