Reading xml data using classic ASP

asp-classic, xml

Solution

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` and `selectSingleNode` instead of`getElementsByTagName`. The `getElementsByTagName` 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.

Problem

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> ```

Original source