ASP Using XML Problem

M

Matt

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

ljb

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
 
L

ljb

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

Matt

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?
 
L

ljb

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top