阅读使用传统的ASP XML数据 [英] Reading xml data using classic ASP
本文介绍了阅读使用传统的ASP XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经写了code在传统的ASP读取XML数据如下:
<%
昏暗objxml
设置objxml =的Server.CreateObject(Microsoft.XMLDOM)
objxml.async =假
objxml.load(/abc.in/xml.xml)设置ElemProperty = objxml.getElementsByTagName(产品)
设置的Elemen = objxml.getElementsByTagName(产品/产品code)
设置Elemtown = objxml.getElementsByTagName(产品/产品名称)
设置Elemprovince = objxml.getElementsByTagName(产品/ productSku,则)的Response.Write(ElemProperty)
的Response.Write(的Elemen)
的Response.Write(Elemprovince)
对于i = 0到(ElemProperty.length -1) 回复于产品code =
的Response.Write(的Elemen)
回复于产品名称=
的Response.Write(Elemtown)及< BR>中
的Response.WriteproductSku,则=
的Response.Write(Elemprovince)及< BR>中下一个设置objxml =什么
%GT;
这code未给予适当的输出。
请帮助我。
XML是:
<产品与GT;
<产品code基ABC< /产品code>
<&产品名称GT; CC斯凯铰链手镯袖口扣在黑与LT; /产品名称>
< /产品与GT;
解决方案
试试这个:
<%设置objXMLDoc =的Server.CreateObject(MSXML2.DOMDocument.3.0)
objXMLDoc.async = FALSE
objXMLDoc.load使用Server.Mappath(/ abc.in/xml.xml)XMLPRODUCT暗淡
对于每个XMLPRODUCT在objXMLDoc.documentElement.selectNodes(产品)
昏暗的产品code:产品code = xmlProduct.selectSingleNode(产品code)文本
昏暗产品名称:产品名称= xmlProduct.selectSingleNode(产品名称)的文本。
回复于Server.HTMLEn code(产品code)及
回复于Server.HTMLEn code(产品名称)及< BR>中
下一个%GT;
注:
- 请不要使用Microsoft.XMLDOM使用显式MSXML2.DOMDocument.3.0
- 使用
使用Server.Mappath
来解决虚拟路径 - 使用
的selectNodes
和的selectSingleNode
而不是的getElementsByTagName
的。在的getElementsByTagName
扫描所有后代等都可以返回意外的结果,然后你总是需要索引的结果,即使你知道你希望只有一个返回值。 - 始终
Server.HTMLEn code
数据发送到响应的时候。 - 请不要把()的怪异的地方,这是没有的VBScript JScript中。
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.
The xml is:
<Product>
<ProductCode>abc</ProductCode>
<ProductName>CC Skye Hinge Bracelet Cuff with Buckle in Black</ProductName>
</Product>
解决方案
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.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数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文