Using Data Source Objects

There are four DSOs in Internet Explorerthe Microsoft HTML (MSHTML) control, the tabular data control (TDC), the XML DSO, and XML data islands. (In fact, Internet Explorer also supports the relatively sophisticated Remote Data Service (RDS) DSO, which you use to connect to database applications, such as those that run in SQL-enabled applications on a Web server.) Two of these DSOs, the XML DSO and XML data islands, support XML documents.

A DSO doesn't appear in a Web page (although, as we'll see at the end of the chapter, the XML DSO can display status messages in a page). You use a DSO to read a document and make its data available to the rest of the page. For a DSO to read data from a document, that data must be formatted correctly.

Here's an example HTML document, ch08_01.html, that holds data on sales made to customers. Here, I'm recording the customers' names , their IDs, the date of the purchase, the department of the item purchased, and the name of the item. Note how I'm structuring an HTML page to hold data for Internet Explorerby using the <SPAN> element (you can also use other elements such as <DIV> ) and assigning a type to each data item with the enclosing element's ID tag (as you can see, this technique cries out for XML formatting):

Listing ch08_01.html
 <HTML>     <HEAD>         <TITLE>             Customer Data         </TITLE>     </HEAD>     <BODY>         Name: <SPAN ID="NAME">Charles</SPAN><BR>         ID: <SPAN ID="CUSTOMER_ID">58704</SPAN><BR>         Purchase date: Date: <SPAN ID="PURCHASE_DATE">             10/15/2003</SPAN><BR>         Department: <SPAN ID="DEPARTMENT">             Meat</SPAN><BR>         Product: <SPAN ID="PRODUCT_NAME">Ham</SPAN><BR>         Name: <SPAN ID="NAME">Franklin</SPAN><BR>         ID: <SPAN ID="CUSTOMER_ID">58705</SPAN><BR>         Purchase date: <SPAN ID="PURCHASE_DATE">             10/15/2003</SPAN><BR>         Department: <SPAN ID="DEPARTMENT">             Produce</SPAN><BR>         Product: <SPAN ID="PRODUCT_NAME">Tomatoes</SPAN><BR>         Name: <SPAN ID="NAME">Phoebe</SPAN><BR>         ID: <SPAN ID="CUSTOMER_ID">58706</SPAN><BR>         Purchase date: <SPAN ID="PURCHASE_DATE">             10/15/2003</SPAN><BR>         Department: <SPAN ID="DEPARTMENT">             Meat</SPAN><BR>         Product: <SPAN ID="PRODUCT_NAME">Turkey</SPAN><BR>         Name: <SPAN ID="NAME">Mark</SPAN><BR>         ID: <SPAN ID="CUSTOMER_ID">58707</SPAN><BR>         Purchase date: <SPAN ID="PURCHASE_DATE">             10/15/2003</SPAN><BR>         Department: <SPAN ID="DEPARTMENT">             Meat</SPAN><BR>         Product: <SPAN ID="PRODUCT_NAME">Beef</SPAN><BR>         Name: <SPAN ID="NAME">Nancy</SPAN><BR>         ID: <SPAN ID="CUSTOMER_ID">58708</SPAN><BR>         Purchase date: <SPAN ID="PURCHASE_DATE">             10/15/2003</SPAN><BR>         Department: <SPAN ID="DEPARTMENT">             Frozen</SPAN><BR>         Product: <SPAN ID="PRODUCT_NAME">Broccoli</SPAN><BR>         </BODY>     </HTML> 

The most common way of handling data formatted as HTML documents is with the MSHTML DSO, and I'm going to use it here. A DSO reads a document like this one and converts it into a recordset. Each record in the recordset comes from the HTML or XML elements you've used to store the data. For example, here's what the HTML for one record looks like:

 Name: <SPAN ID="NAME">Charles</SPAN><BR>  ID: <SPAN ID="CUSTOMER_ID">58704</SPAN><BR> Purchase date: Date: <SPAN ID="PURCHASE_DATE">     10/15/2003</SPAN><BR> Department: <SPAN ID="DEPARTMENT">     Meat</SPAN><BR> Product: <SPAN ID="PRODUCT_NAME">Ham</SPAN><BR> 

This record has five fields: NAME , CUSTOMER_ID , PURCHASE_DATE , DEPARTMENT , and PRODUCT_NAME . A recordset is much like an array holding records; when you work with a particular record, you can access the data in the fields in each record individually. For example, to determine what item Charles has purchased, you only need to find his record and then check the PRODUCT_NAME field in that record.

Using the Internet Explorer <OBJECT> element, you can create an MSHTML DSO and bind it to employee.htm. Here, I'm naming this DSO dsoCustomer :

 <OBJECT ID="dsoCustomer" DATA="ch08_01.html" HEIGHT="0" WIDTH="0">  </OBJECT> 

The DSO will read and interpret ch08_01.html and convert that document into an ADO recordset (the type of recordset actually used in Internet Explorer is read-only, called an ADOR recordset). The DSO holds data from only one record at a time, and that record is called the current record. You can use the built-in methods of a recordset to navigate through your data by making other records the current record. Some common methods are moveFirst , moveLast , moveNext , and movePrevious , which let you navigate from record to record, and we'll see these methods here. To actually display the data from this DSO, you can bind it to HTML elements.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net