ASP
  Home arrow ASP arrow Page 3 - XML Strengths and Weaknesses with DOM, ASP...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP

XML Strengths and Weaknesses with DOM, ASP and XSL
By: Nakul Goyal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 17
    2002-03-05

    Table of Contents:
  • XML Strengths and Weaknesses with DOM, ASP and XSL
  • XML Definitions
  • XML and DOM
  • Our XML example explained
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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 ASP

    The 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

    Loop


    One 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:

    The result of combining the XML with an XSL stylesheet

    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.

    More ASP Articles
    More By Nakul Goyal


     

    ASP ARTICLES

    - Central Scoreboard with Flash and ASP
    - Calorie Counter Using WAP and ASP
    - Creating PGP-Encrypted E-Mails Using ASP
    - Be My Guest in ASP
    - Session Replacement in ASP
    - Securing ASP Data Access Credentials Using t...
    - The Not So Ordinary Address Book
    - Adding and Displaying Data Easily via ASP an...
    - Sending Email From a Form in ASP
    - Adding Member Services in ASP
    - Removing Unconfirmed Members
    - Trapping HTTP 500.100 - Internal Server Error
    - So Many Rows, So Little Time! - Case Study
    - XDO: An XML Engine Class for Classic ASP
    - Credit Card Fraud Prevention Using ASP and C...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT