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 Gayathri Gokul Rating: / 13 April 05, 2004
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.
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:
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.
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.
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:
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.
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.
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 Response, adPersistXML
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:
binding hierarchical XML data, such as those in data shaped recordsets
enumerated the working of XML data with Data Island
persisting ADO recordsets to XML files as well as streams
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.