|
XML Strengths and Weaknesses with DOM,
ASP and XSL
Author: Nakul Goyal
Download Source Files
Firstly, we connect to SQL Server 2000 using a system DSN.
The DSN is called "pubs", and you should create the DSN using control panel. It
should connect to your SQL server, more specifically to its pubs database. We instantiate
an ADO connection object, passing in the DSN to its open method:
'Open database connection
Set conn = Server.CreateObject("ADODB.Connection")
dsn = "DSN=pubs;UID=sa;PWD="
conn.Open dsn
Once we're connect to our database, we create a new XML document and assign it a
root element called "Hi-Tech". We then proceed to retrieve a recordset from the
authors table of our pubs database:
If (xmldoc.childNodes.length = 0) Then
' Build the XML document
Set root = xmldoc.createNode("element", "Hi-Tech", "")
xmldoc.appendChild (root)
' Queries the database for customer data
Sql = "select au_lname,au_fname,au_id from authors"
Set rs = conn.Execute(Sql)
rs.MoveFirst
We then loop through each record in the recordset, appending its title, titleID
and royalty fields to the XML document that we created earlier. We use the createNode
and appendChild methods to do so:
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)
Once we've retrieved each of the records from the recordset and appended them to
our XML document, we save the XML document to our local machine using MSXML's save
method:
xmldoc.save server.mappath("saved.xml")
We're now at the point where we have an XML file called saved.xml, as well as the
style sheet that's included in the support material for this article, called saved.xml.
We instantiate a new XMLDOM object for each of these files, calling the transformNode
method of the XML DOM object with a reference to the XML DOM object that contains
the XSL file:
sourceFile = Server.MapPath("saved.xml")
styleFile = Server.MapPath("saved.xsl")
set source = Server.CreateObject("Microsoft.XMLDOM")
source.async = false
source.load(sourceFile)
set style = Server.CreateObject("Microsoft.XMLDOM")
style.async = false
style.load(styleFile)
Lastly, we use Response.Write to output the transformed XML to the browser:
Response.Write source.transformNode(style)
Well, I hope this article has answered some of your questions on XML. Hopefully
you've learned a thing or two about the advantages and disadvantages of using XML,
when it can be used, and most importantly, how it can be used.
Nakul Goyal, currently doing Master of Sciences in Information
Technology from Panjab University, Chandigarh. A Bachelor of Computer Applications
from Punjab Technical University, he is passionate towards the Cyber World &
he likes to write about Technology. He's also a Microsoft Certified Professional
and a Brainbench Certified 'MVP' (Most Valuable Professional). Also the Co-Founder
of CWSTeam (http://www.cwsteam.com).
Contact Nakul Goyal by Email: nakul@cwsteam.com.
View Nakul's Homepage at http://www.nakul.NET
Have Questions? Discuss this topic in Dev
Forum
|