ASP Using XML Problem

Discussion in 'ASP General' started by Matt, Feb 19, 2004.

  1. Matt

    Matt Guest

    I want the ASP page retrieves data from XML file and put in table. However,
    is it possible that the ASP code doesn't know the look of XML file
    structure? The following is my ASP code and XML data file. If more levels
    are added in XML file, I need to change ASP code as well. i.e. For every
    additional level in XML file, I need to add an additional loop in ASP page.

    My question is: is it possible that ASP code is unchanged, even XML file
    structure has changed. I just want the ASP code traverses the XML file can
    print out all data in the table.

    Please advise. Thanks!

    ==============================================================
    <%
    Sub GetXMLData(link)
    Dim xml
    Set xml = Server.CreateObject("Microsoft.XMLDOM")
    xml.async = False
    xml.load(Server.MapPath(link))
    Dim root
    Set root = xml.documentElement
    %>
    <table border="1">
    <th>name</th><th>value</th>
    <tbody>
    <%
    Dim name, url, thisChild
    For I = 0 TO (root.childNodes.length - 1) '# level2
    Set thisChild = root.childNodes(I)
    For J = 0 TO (thisChild.childNodes.length - 1) '# level 3
    Set thisChild2 = thisChild.childNodes(J)
    name = thisChild2.childNodes(0).Text
    value = thisChild2.childNodes(1).Text
    %>
    <TR><TD><%= name %></TD><TD><%= value %></TD></TR>
    <% Next %>
    <% Next %>
    </tbody></table>
    <%
    End Sub
    %>

    <%
    Call GetXMLData("xmldata.xml")
    %>

    ==============================================================
    <!-- xmldata.xml -->

    <?xml version="1.0"?>
    <level1>
    <level2>
    <level3>
    <name>name1</name>
    <value>value1</value>
    </level3>
    <level3>
    <name>name2</name>
    <value>value2</value>
    </level3>
    </level2>
    <level2>
    <level3>
    <name>name3</name>
    <value>value3</value>
    </level3>
    </level2>
    </level1>
    Matt, Feb 19, 2004
    #1
    1. Advertising

  2. Matt

    ljb Guest

    You are making your life too hard. Use XSL to output the table. You will
    need a variation of this using MSXML 4.0

    Set xml = CreateObject("Msxml2.DOMDocument.4.0")
    Set xsl = CreateObject("Msxml2.DOMDocument.4.0")

    xml.async = False
    xsl.async = false

    xml.Load "xmldata.xml"
    xsl.Load "name table.xsl"

    set ofile = CreateObject("ADODB.Stream")
    ofile.Type = 2
    ofile.Charset = "Windows-1252"
    ofile.LineSeparator = -1
    ofile.open

    xml.transformNodeToObject xsl, ofile 'produces UTF-8

    ofile.SaveToFile "name table.htm", 2


    "Matt" <> wrote in message
    news:...
    > I want the ASP page retrieves data from XML file and put in table.

    However,
    > is it possible that the ASP code doesn't know the look of XML file
    > structure? The following is my ASP code and XML data file. If more levels
    > are added in XML file, I need to change ASP code as well. i.e. For every
    > additional level in XML file, I need to add an additional loop in ASP

    page.
    >
    > My question is: is it possible that ASP code is unchanged, even XML file
    > structure has changed. I just want the ASP code traverses the XML file can
    > print out all data in the table.
    >
    > Please advise. Thanks!
    >
    > ==============================================================
    > <%
    > Sub GetXMLData(link)
    > Dim xml
    > Set xml = Server.CreateObject("Microsoft.XMLDOM")
    > xml.async = False
    > xml.load(Server.MapPath(link))
    > Dim root
    > Set root = xml.documentElement
    > %>
    > <table border="1">
    > <th>name</th><th>value</th>
    > <tbody>
    > <%
    > Dim name, url, thisChild
    > For I = 0 TO (root.childNodes.length - 1) '# level2
    > Set thisChild = root.childNodes(I)
    > For J = 0 TO (thisChild.childNodes.length - 1) '# level 3
    > Set thisChild2 = thisChild.childNodes(J)
    > name = thisChild2.childNodes(0).Text
    > value = thisChild2.childNodes(1).Text
    > %>
    > <TR><TD><%= name %></TD><TD><%= value %></TD></TR>
    > <% Next %>
    > <% Next %>
    > </tbody></table>
    > <%
    > End Sub
    > %>
    >
    > <%
    > Call GetXMLData("xmldata.xml")
    > %>
    >
    > ==============================================================
    > <!-- xmldata.xml -->
    >
    > <?xml version="1.0"?>
    > <level1>
    > <level2>
    > <level3>
    > <name>name1</name>
    > <value>value1</value>
    > </level3>
    > <level3>
    > <name>name2</name>
    > <value>value2</value>
    > </level3>
    > </level2>
    > <level2>
    > <level3>
    > <name>name3</name>
    > <value>value3</value>
    > </level3>
    > </level2>
    > </level1>
    >
    >
    >
    >
    ljb, Feb 19, 2004
    #2
    1. Advertising

  3. Matt

    ljb Guest

    I'm not sure I understand your question. ASP is used to generate an HTML or
    other stream that is sent to the requesting application. The whole HTML
    document is generated with each request. Perhaps some of it could come from
    include files that don't change.

    I'm not an expert in XML/XSL. They are over in the microsoft.public.xsl news
    group. However I use it whenever I can. Perhaps xml.transformNodeToObject or
    something similar could be directed to a response.write and a file not
    created.

    You should be able to run the vbscript from my earlier message and this XSL
    file from your desktop and experiment with it. If your level numbers change
    the XSL will need to be adjusted unless someone can show us a more generic
    way of doing it and I'm sure someone can.

    ------------"name table.xsl"-------------

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ms="urn:schemas-microsoft-com:xslt">

    <xsl:eek:utput method="html" encoding="UTF-8" indent="yes" />

    <xsl:template match="/">
    <html>
    <body>
    <table>

    <xsl:for-each select="//level3">
    <tr>
    <td><xsl:value-of select="name" /></td>
    <td><xsl:value-of select="value" /></td>
    </tr>
    </xsl:for-each>

    </table>
    </body>
    </html>
    </xsl:template>

    </xsl:stylesheet>
    ljb, Feb 19, 2004
    #3
  4. Matt

    Matt Guest

    When I try to run, there is error for 'Msxml2.DOMDocument.4.0'

    Microsoft VBScript runtime (0x800A01AD)
    ActiveX component can't create object: 'Msxml2.DOMDocument.4.0'

    any ideas?
    Matt, Feb 20, 2004
    #4
  5. Matt

    Mark Schupp Guest

    Are you sure you have MSXML4 installed?

    Try "Msxml2.DOMDocument"

    --
    Mark Schupp
    Head of Development
    Integrity eLearning
    www.ielearning.com


    "Matt" <> wrote in message
    news:...
    > When I try to run, there is error for 'Msxml2.DOMDocument.4.0'
    >
    > Microsoft VBScript runtime (0x800A01AD)
    > ActiveX component can't create object: 'Msxml2.DOMDocument.4.0'
    >
    > any ideas?
    Mark Schupp, Feb 20, 2004
    #5
  6. Matt

    ljb Guest

    Try Msxml2.DOMDocument.3.0 or Msxml2.DOMDocument. If you have IE6 installed
    you should have MSXML3 also. I verified the transform works in 3 but some
    earlier versions of MSXML will not.

    "Matt" <> wrote in message
    news:...
    > When I try to run, there is error for 'Msxml2.DOMDocument.4.0'
    >
    > Microsoft VBScript runtime (0x800A01AD)
    > ActiveX component can't create object: 'Msxml2.DOMDocument.4.0'
    >
    > any ideas?
    ljb, Feb 23, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. loveNUNO
    Replies:
    2
    Views:
    892
    loveNUNO
    Nov 20, 2003
  2. Tony Prichard
    Replies:
    0
    Views:
    715
    Tony Prichard
    Dec 12, 2003
  3. jkflens
    Replies:
    2
    Views:
    1,446
    jkflens
    May 30, 2006
  4. Matt
    Replies:
    3
    Views:
    325
    Bob Barrows [MVP]
    Apr 23, 2004
  5. Erik Wasser
    Replies:
    5
    Views:
    429
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page