When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Thursday, October 12, 2000

Accessing XML Data using ASP, Part 2
By Bipin Joshi


  • Read Part 1

  • In Part 1 we looked at accessing stand alone XML files. In this part, we'll examine three other alternatives to accessing XML data!

    - continued -

    Creating XML files from ground up
    Some times you may want to build entire XML document in memory by yourself. This can be the case if your data is stored in RDBMS and you want to convert in into XML only for processing. You can create XML in memory in two ways:

      1. Using XMLDOM methods like createElement() and appendChild()
      2. Directly loading XML data in the form of a string

    Let's look at using the XMLDOM first:

    Dim mydoc,myelement
    Set mydoc=Server.CreateObject("Microsoft.XMLDOM")
    
    set myelement=mydoc.createElement("rootelement")
    mydoc.appendChild(myelement)
    

    The code for loading the XML data from a string appears as follows:

    Dim mydoc,strXML
    Set mydoc=Server.CreateObject("Microsoft.XMLDOM")
    
    strXML="<book><author>author1</author><title>title1</title></book>"
    mydoc.loadXML(strXML)
    

    Sending XML data to client
    We will consider following three cases:

      1. You have generated a full XML document and sending it to the client as is. Here you can include a stylesheet reference in the XML itself and browser will apply that stylesheet to your document. This requires that the client browser supports XML.
      2. You have generated XML data but want to send it as HTML to the client. Here you can generate a simple HTML by applying a stylesheet at server side.This do not enforce client browser support for XML
      3. You want to send XML data as Data Islands to the client. Here you can use <XML> tag with an ID attribute or <SCRIPT LANGUAGE=XML>. IE also provides a way to directly bind data islands to HTML elements, liks TABLEs.

    Now, let's look at code for each of the three scenarios described above. Code for the first method follows below:

    Dim mydoc,myelement
    Set mydoc=Server.CreateObject("Microsoft.XMLDOM")

    mydoc.load("xml_file_path")
    Response.ContentType = "text/xml"

    Response.write mydoc.xml

    Or

    Response.write "xml_as_string"

    Here we set ContentType to text/xml so that an XML-aware client browser knows what to do with it. We can also send raw XML in the form of string:

    Dim myXML,myXSL
    myXML=Server.CreateObject("Microsoft.XMLDOM")
    myXSL=Server.CreateObject("Microsoft.XMLDOM")
    
    'Fill myXML with one of the methods mentioned above
    
    'Load a stylesheet
    myXSL.load "xsl_path"
    
    strHTML = mydoc.transformNode(myXSL.documentElement)
    Response.write strHTML
    

    Finally, using the third method (using the <XML> tag with an ID attribute or <SCRIPT LANGUAGE=XML>), we have:

    <XML ID=mydata>
      <node1>
        <child1>Some data</child1>
        <child2>Some other data</child2>
          ...
      </node1>
    </XML>
    

    Now, you can bind a table to this data island like this:

    <table datasrc="#mydata">
     <tr>
       <td>
         <div datafld="child1"></div>
       </td> 
       <td>
         <div datafld="child2"></div>
       </td> 
      </tr>
    </table> 
    

    The table will display all the rows from the data island automatically.

    Storing XML data
    Suppose you want to save XML data - generated by you or posted by a client - to a disk file. This is not too difficult, simply call the save() method of Document object as follows.

    Dim mydoc,strXML
    Set mydoc=Server.CreateObject("Microsoft.XMLDOM")
    
    strXML="<book><author>author1</author><title>title1</title></book>"
    
    mydoc.loadXML(strXML)
    mydoc.save("path_goes_here")
    

    Note: With IIS 5.0 you can also save the XML data to ASP Response object directly with mydoc.save(Response). This is useful if you are processing the XML data and sending it back to client.

    Well, I hope you have found this article useful. To summarize, we examined a number of ways to access/create/display XML data from an ASP page. Happy Programming!

  • By Bipin Joshi


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES