locked
Parse XML in classic ASP RRS feed

  • Question

  • User-1979539317 posted

    I'm trying to parse XML in classic ASP. The following code displays the XML file sequentially. How do I randomly access the fields <Product> and <SKU>? I've been trying to use getElementsByTagName method although I'm unable to get this working properly.

    <%
    Dim mydoc
    Set mydoc=Server.CreateObject("Microsoft.XMLDOM")
    
    mydoc.load(Server.MapPath("xml.xml"))
    set nodes = mydoc.selectNodes("//*")
    
    for i = 0 to nodes.length - 1
        response.write(nodes(i).nodeName & " - " & nodes(i).text & "<br />")
    next
    %>
    
    
    xml file
    --------
    <?xml version="1.0" standalone="yes"?>
    <NewDataSet>
      <Table>
        <Product>Test1</Product>
        <SKU>AAA</SKU>
      </Table>
      <Table>
        <Product>Test2</Product>
        <SKU>BBB</SKU>
      </Table>
    </NewDataSet>

    Sunday, August 6, 2017 3:39 PM

All replies

  • User-460007017 posted

    Hi fraser1,

    I use the JS and getElementsByTagName achieved the same requirement:

    <html>
    <body>
    <script type="text/javascript">
    try //Internet Explorer
      {
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      }
    catch(e)
      {
      try //Firefox, Mozilla, Opera, etc.
        {
        xmlDoc=document.implementation.createDocument("","",null);
        }
      catch(e) {alert(e.message)}
      }
    try 
      {
      xmlDoc.async=false;
      xmlDoc.load("xml.xml");
      document.write("xmlDoc is loaded, ready for use");
      }
    catch(e) {alert(e.message)}
    document.write("<br />");
    x= xmlDoc.getElementsByTagName("Product");
    y= xmlDoc.getElementsByTagName("SKU");
     
      for (i=0;i<x.length;i++)
    {
    
    document.write(x[i].childNodes[0].nodeValue);
    
    document.write("-");
    document.write(y[i].childNodes[0].nodeValue);
    
    document.write("<br />");
    }
    
    </script>
    </body>
    </html>

    Best Regards,

    Yuk Ding

    Monday, August 7, 2017 8:11 AM
  • User-1979539317 posted

    What's the below code translated into classic ASP?

    x= xmlDoc.getElementsByTagName("Product");
    x[i].childNodes[0].nodeValue

    Monday, August 7, 2017 12:37 PM
  • User-460007017 posted

    Hi fraser1,

    I find the similar classic ASP code with your issue:

    http://www.sitepoint.com/forums/showthread.php?885705-Reading-XML-File-With-Classic-ASP

    Best Regards,

    Yuk Ding 

    Tuesday, August 8, 2017 2:42 AM
  • User-1979539317 posted

    Is it possible to access the fields directly using?

    xmlDoc.getElementsByTagName("Product");

    or does it always have to be through

    x[i].childNodes[0].nodeValue

    Thursday, August 10, 2017 12:55 PM
  • User-460007017 posted

    Hi fraser1,

    Considering The product element doesn't have any attribute, I think it should be the simplest way.

    Best Regards,

    Yuk Ding

    Wednesday, August 16, 2017 8:20 AM