XML Integration with ADO

In a previous article we looked at XML integrating with ADO and IE and some of the nice features that allow us to access a recordset as an XML document. In today’s tutorial we will be covering multiple levels of hierarchical XML data,illustrating some complex XML data binding examples and enumerating how to persist ADO recordsets to XML files as well as streams. We also discuss a much easier way of persisting ADO recordsets to response objects.

Contributed by
Rating: 3 stars3 stars3 stars3 stars3 stars / 13
April 05, 2004
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Let us dive right into the XML data pool and soak ourselves with a few sample codes. For starter consider the following code:


<XML ID “diDataPublishers”>
  
<Publishers>
     
<Publisher>
        
<pub_id>001</pub_id>
         
<pub_name>McGraw Hill</pub_name>
        
<city>Berkeley</city>
         
<state>CA</state>
         
<country>USA</country>
          
<title>
               
<title_id>Mc1</title_id>
               
<title>HTML-Virtual Classroom.</title>
               
<price>$39.99</price>
               
<pub_date>5/24/02</pub_date>
           
<sale>
               
<store_id>9898</store_id>
               
<ord_num>H001</ord_num>
               
<ord_date>$39.99</ord_date>
               
<qty>30</qty>
           
</sale>
       
</title>
       
<employee>
               
<emp_id>JM01B</emp_id>
                 
<fnameHenry<fname>
                  
<midinl>M</midinl>
                  
<lname>Ford<lname>
                   
<emp_title>Manager</emp_title>
         
</employee>
        
<employee>
               
<emp_id>WC02S</emp_id>
                 
<fname>William<fname>
                  
<midinl>C</midinl>
                  
<lname>Smith<lname>
                   
<emp_title>Cashier</emp_title>
        
</employee>

XML Data Simplified

The following diagram contains different levels of data. First we have the Publishers, then for each publisher a list of employees and a list of sales. Subsequently for each sale, there is a list of books that were sold.

XML ADO

The above diagram is self explanatory.  Let’s imagine that we have a hierarchical database in XML, say we want to enable it all to be viewed at once in the browser. What we are trying to achieve here is a top-level table containing just the Publishers. For each publisher, there should be two sub-tables. One is holding the values for the employee and other sales. For each sale, we try to maintain another table containing the details of the books sold. Let us assume each of these tables have been populated with some data.

From the above diagram it should be clear that, the only way we can bind to these relatively simple multi layers is to layer our binding. For the above XML, we would do the following to give nested tables. Let’s examine Example 2:


<TABLE ID “tbPublishers” DATASRC=  #diPublishers” BORDER= “1”>
  <TBODY>
     <TR>
               <TD><DIV DATAFLD= “pub_name”</DIV></TD>
               <TD><DIV DATAFLD= “city”</DIV></TD>
              <TD><DIV DATAFLD= “state”</DIV></TD>
         </TR>
         <TR>
      <TD COLSPAN=4>
       <TABLE >
          <TR>
              <TD>Titles</TD>
            </TR>
            <TR>
              <TD COLSPAN=4>
               <TABLE  DATASRC= “#diPublishers” DATAFLD = “Title” Border= “1”>
                <TR>
                      <TD><DIV DATAFLD= “title”</DIV></TD>
                        <TD><DIV DATAFLD= “price”</DIV></TD>
                 </TR>
            <TR>
              <TD COLSPAN=3>
                  <TABLE  DATASRC= “#diPublishers” DATAFLD = “sale” Border= “1”>
                <TR>
                      <TD><DIV DATAFLD= “ord_date”</DIV></TD>
                        <TD><DIV DATAFLD= “qty”</DIV></TD>
                     </TR>
                 </TABLE>
               </TD>
            </TR>
        </TABLE>
        </TD>
   </TR>
   <TR>
      <TD>Employee</TD>
   </TR>
   <TR>
       <TD COLSPAN =4>
     <TABLE  DATASRC= “#diPublishers” DATAFLD = “Employee” Border= “1”>
               <TR>
                   <TD><DIV DATAFLD= “fname”</DIV></TD>
                    <TD><DIV DATAFLD= “lname”</DIV></TD>
                     </TR>
                 </TABLE>


Easy as 1,2,3

We have several bound tables, using successive levels in the XML data as the source of the data. So we would end up with something that looks like below on IE, when we load BindingHierarchialData.html.

XML ADO

All that we have done is add some bound tables to HTML. The Data Island supplies the data. If you think that the above output doesn’t look so dignified, don’t worry. With a little help formatting we can set things straight and achieve really good-looking results. Now we have a Publishers table populated with all the above-specified fields and each publisher with a list of books published.

Assume the Publisher, “McGraw Hill” has the following books published: HTML-Virtual Classroom, Java In 21 days, SQL Server Programming, Instant ASP Scripts and XML Complete. For each publisher, there should be two sub-tables. One should hold the populated values for the employee and the other populated with values of sales. For each sale, we try and maintain another table containing the details of the books sold. This too has been populated.

ADO XML

Levels of Hierarchy

If you find HTML data binding is getting a little confusing you can include the levels of hierarchy in the DATAFLD attribute to make life much simpler and a little clearer. For example let us consider the following example 2:


<TABLE ID “tblPublishers” DATASRC #diPublishers”>
    <TABLE DATASRC = “#diPublishers” DATAFLD = “Title”>
         <TABLE DATASRC = “#diPublishers” DATAFLD = “Title.sale”>
<TABLE DATASRC = “#diPublishers” DATAFLD = “Employee”

The above change doesn’t change the way anything works, but it clarifies where “sale” files sit in the hierarchy.

Saving Recordsets as XML

Using ADO we can very easily generate XML recordsets by making use of  “Save” method of the Recordset object, and specifying the format as “adPersistXML”. This feature isn’t new to ADO 2.5, but what is novel is the ability to persist hierarchical recordsets, and to directly persist them into other objects, such as DOM objects and streams.

This opens up any number of possibilities for manipulation and transfer of data, since we are no longer restricted to creating text files containing XML data. Why is this such good news? Well, as programmers we want two major things to work without hassle:

  • An easy way to generate data.
  • A fast way to transfer data.

Creating text files is easy, but slow. It involves tedious work like writing the file to the disk, and then reading the data back in to manipulate it. What we are trying to reach ultimately is finding out ways to eliminate as much as the slow processing as possible. If we could derive at anything that makes our life easier as programmer or developers has to be good thing.

Persisting ADO Recordsets to XML Files

The simplest method of converting ADO recordset to XML is a method that’s been in previous version of ADO. Just included this code for reference.

<pre><blockquote><font face=verdana size=1>
rs.Authors.Save  “c:tempauthors.xml”
</font></blockquote></pre>

Persisting ADO Recordsets to a Stream

A Stream is simply a block of data in memory, which is not processed in any way. ADO 2.5 introduced the new Stream object, and this can be the way for saving a recordset.


Set rsAuthors Server.CreateObject(ADOBD.Recordset”);
Set stmAuthors 
Server.CreateObject(ADOBD.Stream”);
rsAuthors.Open “authors”strConn
rsAuthors
.Save  stmAuthorsadpersistXML

We now have a Stream object containing the XML recordset, and we can use the stream methods and properties to manipulate data. Let’s say we want to extract the XML into a string using the ReadText method, we could do so as follows:


strXMLAuthors stmAuthors.ReadText.

At this stage strXMLAuthors contains the complete XML recordset including the schema.

Persisting ADO Recordsets to the Response Object

We can think of streams as something more literal. The dictionary defines a stream as "a flow or moving succession of anything." So, let's think of a stream of data as a flow of data from one place to another. Keeping this in mind, let’s imagine we are sending this data from our ASP page to browser-that’s a flow of data, too.

Just how do we do this?  We do so by using a new feature of the Response object, which has built in support for Streams. So it is like pouring our data into one end of the stream, and having it end up at the other end. On one end is the Response Object, and the other end is the browser. All this is made feasible because of the integrated support for streams in the Response object:


rsAuthors.Save ResponseadPersistXML

This saves the recordset as XML directly into the response object. The Response object in turn handles the sending of data to the browser. Pretty cool!

Conclusion

Today we have learned about:

  1. binding hierarchical XML data, such as those in data shaped recordsets
  2. enumerated the working of XML data with Data Island
  3. persisting ADO recordsets to XML files as well as streams
  4. a easier way of Persisting ADO recordsets to Response objects.

Each one has its own advantages over the other. But there is a much easier way to do this, which is Persisting ADO Recordsets to DOM object, which we will be covering in depth in the next part of the tutorial.

blog comments powered by Disqus
XML ARTICLES

- More on Triggers and Styles and Control Temp...
- Looking at Triggers with Styles and Control ...
- A Closer Look at Styles and Control Templates
- Styles and Control Templates
- Properties and More in XAML
- Elements and Attributes in XAML
- XAML in a Nutshell
- Importing XML Files into Access 2007
- Using MSXML3.0 with VB 6.0
- MSXML, concluded
- MSXML, continued
- MSXML Tutorial
- Generating XML Schema Dynamically Using VB.N...
- XSL Transformations using ASP.NET
- Applying XSLT to XML Using ASP.NET

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials