PDA

View Full Version : Trying to parse a XML file in ASP


zesolution
04-01-2005, 07:13 AM
Greetings community.

I am very very new at ASP. Im a PHP guy.

For a new job, I need to parse this simple XML file in ASP, load each nodes into an multidimensionnal array, in order to loop through this array and display a HTML template loaded with the data.

Simple enough ? Well, I have no clue how to achieve this in ASP.

Ive been through the methods and properties of Miscrosoft.XMLDOM (found here : http://www.adp-gmbh.ch/web/js/msxmldom/methods_properties.html), but I really can go no further then this piece of code :

<%
Dim strXMLFile
Dim objXML
Dim objXSL

strXMLfile = "a.xml"
set objXML = Server.CreateObject("Microsoft.XMLDOM")
objXML.async = false
objXML.load(strXMLFile)
%>

Now what ?? : P

Here is a sample XML file. Any help would be very appreciated.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<liste alpha="A" dateMAJ="2002/07/08">
<expression id="1477" date="2002/07/08" pub="1" lettre="A">
<texte>sometext</texte>
<correction>sometext</correction>
</expression>
<expression id="1476" date="2002/07/08" pub="1" lettre="A">
<texte>On a eu un « bref aperçu » du spectacle.</texte>
<correction></correction>
</expression>
</liste>

xtreme
04-26-2005, 03:31 PM
Hi,

Have you thought about using an XSLT instead of looping through your document....

Your ASP would look something like this....


<html>
<head></head>
<body leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0">
<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="100%" valign="top">
<div id="divMain" style="position:absolute; overflow-y:scroll; overflow-x:auto; width:100%; height:100%; z-index:201;">
<%
Dim gXSLProc
Dim gXSLT
Dim gXSLDoc
Dim gXMLDoc

Set gXMLDoc = Server.CreateObject("MSXML2.FreeThreadedDOMDocument.3.0")

gXMLDoc.async = False
gXMLDoc.validateOnParse = True

gXMLDoc.load "strXMLFile.xml"

Set gXSLDoc = Server.CreateObject("MSXML2.FreeThreadedDOMDocument.3.0")

gXSLDoc.async = False
gXSLDoc.validateOnParse = True

gXSLDoc.load "strXSLFile.xsl"

Set gXSLT = Server.CreateObject("MSXML2.XSLTemplate.3.0")

Set gXSLT.stylesheet = gXSLDoc
Set gXSLProc = gXSLT.createProcessor()

gXSLProc.input = gXMLDoc

gXSLProc.transform()

Response.Write gXSLProc.output

Set gXSLProc = Nothing
Set gXSLT = Nothing
Set gXSLDoc = Nothing
Set gXMLDoc = Nothing
%>
</div>
</td>
</tr>
</table>
</body>
</html>

Youe XSLT would look something like this...
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">

<xsl:template match="/">
<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>COL HEAD1</td>
<td>COL HEAD2</td>
<td>COL HEAD3</td>
</tr>

<xsl:for-each select="//liste">
<tr>
<td><xsl:value-of select="@alpha" /></td>
<td><xsl:value-of select="expression/@date" /></td>
<td><xsl:value-of select="expression/texte" /></td>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


Hope you can figure that out!