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>
2 个解决方案
#1
10
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
- 不要使用微软。XMLDOM使用显式的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. - 使用selectNodes和selectSingleNode而不是getelementsbytagname。getElementsByTagName扫描所有的后代,以便返回意外的结果,然后您总是需要对结果进行索引,即使您知道您只希望返回一个返回值。
- Always
Server.HTMLEncode
data when sending to the response. - 总服务器。发送到响应时的HTMLEncode数据。
- Don't put ( ) in weird places, this is VBScript not JScript.
- 不要把()放在奇怪的地方,这是VBScript而不是JScript。
#2
4
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
#1
10
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
- 不要使用微软。XMLDOM使用显式的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. - 使用selectNodes和selectSingleNode而不是getelementsbytagname。getElementsByTagName扫描所有的后代,以便返回意外的结果,然后您总是需要对结果进行索引,即使您知道您只希望返回一个返回值。
- Always
Server.HTMLEncode
data when sending to the response. - 总服务器。发送到响应时的HTMLEncode数据。
- Don't put ( ) in weird places, this is VBScript not JScript.
- 不要把()放在奇怪的地方,这是VBScript而不是JScript。
#2
4
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