XML Strengths and Weaknesses with DOM, ASP and XSL - XML and DOM
(Page 3 of 5 )
Microsoft have provided us with the MSXML parser, which exposes an XML document in the form of a DOM (Document Object Model). With the XML DOM, you can load and parse XML files, gather information about those files, navigate through and manipulate those files. To learn more about the details of the XML DOM, please refer to
this site.
Now that we've discussed the reasons for using XML, it's time to look at some source code. We will examine some ASP scripts that create and display XML data. We're going to create an XML file using both static data and data from a database using ADO. The DOM methods createNode and appendChild, as well as the text property are used to construct an in-memory XML tree.
XML with ASPThe following example illustrates how to create an XML tree (in memory) and then persist is to disk using the save method:
<%
Dim xmldoc
Set xmldoc = Server.CreateObject("Microsoft.XMLDOM")
' Check to see if document has data. If it does, don't build it
If (xmldoc.childNodes.length = 0) Then
' Build the XML document
Set root = xmldoc.createNode("element", "Hi-Tech", "")
xmldoc.appendChild (root)
Set onode = xmldoc.createNode("element", "Employee", "")
onode.Text = "Gurpreet Singh"
xmldoc.documentElement.appendChild (onode)
Set inode = xmldoc.createNode("element", "Address", "")
onode.appendChild (inode)
Set child = xmldoc.createNode("element", "Address1", "")
child.Text = "Nepean Ont"
inode.appendChild (child)
Set child = xmldoc.createNode("element", "Address2", "")
child.Text = "Canada"
inode.appendChild (child)
End If
xmldoc.save (Server.Mappath("savedI2.xml"))
%>In the example above we create an XMLDOM Object. We then create a root node and its child node using the createNode function. Next, we append the nodes after assigning the text property to each of them. Finally, we save the in-memory XML tree to a file, savedI2.xml.
We can also build an XML file from the results of a database query. I've included two files with the support material for this article: pubtest.asp and saved.xsl. Pubtest.asp connects to the SQL Server 2000 pubs database, retrieving several records from the author's table, formatting them as a new XML document and saving that document as saved.xml.
The saved.xsl file contains an XSL style sheet which is used by pubtest.asp to format saved.xml as HTML. You should download the support material before continuing.
Here's an exert from pubtest.asp:
Do While Not rs.EOF
Set onode = xmldoc.createNode("element", "Employee", "")
xmldoc.documentElement.appendChild (onode)
Set inode = xmldoc.createNode("element", "Name", "")
inode.Text = rs.fields(0) & " " & rs.fields(1)
onode.appendChild (inode)
'Grab another recordset based on the authored
Sql = "select title_id,royaltyper from titleauthor " & _
"where au_id = '" & rs.fields(2) & "'"
Set rs2 = conn.Execute(Sql)
If Not (rs2.EOF = True And rs2.bof = True) Then
Set inode = xmldoc.createNode("element", "Titles", "")
onode.appendChild (inode)
Set child = xmldoc.createNode("element", "TitleId", "")
child.Text = rs2.fields(0)
inode.appendChild (child)
Set child = xmldoc.createNode("element", "royalty", "")
child.Text = rs2.fields(1)
inode.appendChild (child)
rs2.Close
Set rs2 = Nothing
End If
rs.movenext
LoopOne of the XML elements that results from the ASP code shown above looks like this:
<Hi-Tech>
<Employee>
<Name>Bennet Abraham</Name>
<Titles>
<TitleId>BU1032</TitleId>
<royalty>60</royalty>
</Titles>
</Employee>Here's an extract from the XSL style sheet file, saved.xsl:
<xsl:for-each select="Hi-Tech/Employee">
<TR>
<TD WIDTH="40%" BGCOLOR="lightyellow">
<FONT FACE="Verdana" SIZE="2" COLOR="black">
<xsl:value-of select="Name" />
</FONT>
</TD>
<TD WIDTH="30%" BGCOLOR="lightyellow">
<FONT FACE="Verdana" SIZE="2" COLOR="black">
<xsl:value-of select="Titles/TitleId" />
</FONT>
</TD>
<TD WIDTH="30%" BGCOLOR="lightyellow">
<FONT FACE="Verdana" SIZE="2" COLOR="black">
<xsl:value-of select="Titles/royalty" />
</FONT>
</TD>
</TR>
</xsl:for-each>When I ran pubtest.asp in my browser, here's what it looked like:

Let's now run through the entire process of retrieving data from the pubs database, saving it to an XML file, and loading and transforming this document as XSL with the MSXML parser.
Next: Our XML example explained >>
More ASP Articles
More By Nakul Goyal