When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
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



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