XML and Hierarchical Data

One of the most interesting developments in database handling is the capability to create hierarchical recordsets, in which a record can actually contain an entire new recordset. XML documents represent a perfect way of storing hierarchical recordsets because you can enclose one set of elements inside another easily.

Here's an example. In this case, I'm adding records about deliveries made to the customers in ch08_03.xml in a new XML document, ch08_09.xml:

Listing ch08_09.xml
 <?xml version="1.0"?> <CUSTOMERS>     <CUSTOMER>         <NAME>Charles</NAME>         <RECORD>             <CUSTOMER_ID>58704</CUSTOMER_ID>             <PURCHASE_DATE>10/15/2003</PURCHASE_DATE>             <DEPARTMENT>Meat</DEPARTMENT>             <PRODUCT_NAME>Ham</PRODUCT_NAME>  <DELIVERY>   <DATE>10/20/2003</DATE>   <TOTAL_COST>.99</TOTAL_COST>   </DELIVERY>   <DELIVERY>   <DATE>10/25/2003</DATE>   <TOTAL_COST>.49</TOTAL_COST>   </DELIVERY>  </RECORD>     </CUSTOMER>     <CUSTOMER>         <NAME>Franklin</NAME>         <RECORD>             <CUSTOMER_ID>58705</CUSTOMER_ID>             <PURCHASE_DATE>10/15/2003</PURCHASE_DATE>             <DEPARTMENT>Produce</DEPARTMENT>             <PRODUCT_NAME>Tomatoes</PRODUCT_NAME>  <DELIVERY>   <DATE>10/20/2003</DATE>   <TOTAL_COST>.00</TOTAL_COST>   </DELIVERY>   <DELIVERY>   <DATE>10/25/2003</DATE>   <TOTAL_COST>.95</TOTAL_COST>   </DELIVERY>  </RECORD>     </CUSTOMER>     <CUSTOMER>         <NAME>Phoebe</NAME>         <RECORD>             <CUSTOMER_ID>58706</CUSTOMER_ID>             <PURCHASE_DATE>10/15/2003</PURCHASE_DATE>             <DEPARTMENT>Meat</DEPARTMENT>             <PRODUCT_NAME>Turkey</PRODUCT_NAME>  <DELIVERY>   <DATE>10/20/2003</DATE>   <TOTAL_COST>.99</TOTAL_COST>   </DELIVERY>   <DELIVERY>   <DATE>10/25/2003</DATE>   <TOTAL_COST>.99</TOTAL_COST>   </DELIVERY>  </RECORD>     </CUSTOMER>     <CUSTOMER>         <NAME>Mark</NAME>         <RECORD>             <CUSTOMER_ID>58707</CUSTOMER_ID>             <PURCHASE_DATE>10/15/2003</PURCHASE_DATE>             <DEPARTMENT>Meat</DEPARTMENT>             <PRODUCT_NAME>Beef</PRODUCT_NAME>  <DELIVERY>   <DATE>10/20/2003</DATE>   <TOTAL_COST>.95</TOTAL_COST>   </DELIVERY>   <DELIVERY>   <DATE>10/25/2003</DATE>   <TOTAL_COST>.95</TOTAL_COST>   </DELIVERY>  </RECORD>     </CUSTOMER>     <CUSTOMER>         <NAME>Nancy</NAME>         <RECORD>             <CUSTOMER_ID>58708</CUSTOMER_ID>             <PURCHASE_DATE>10/15/2003</PURCHASE_DATE>             <DEPARTMENT>Frozen</DEPARTMENT>             <PRODUCT_NAME>Broccoli</PRODUCT_NAME>  <DELIVERY>   <DATE>10/20/2003</DATE>   <TOTAL_COST>.99</TOTAL_COST>   </DELIVERY>   <DELIVERY>   <DATE>10/25/2003</DATE>   <TOTAL_COST>.99</TOTAL_COST>   </DELIVERY>  </RECORD>     </CUSTOMER> </CUSTOMERS> 

In this case, each <RECORD> element itself contains two <DELIVERY> elements (which contain <DATE> and <TOTAL_COST> elements). A DSO can't simply treat multiple enclosed records like this as a single record because that would give two or more fields in the record the same name. Instead, Internet Explorer makes the recordset into a hierarchical recordset and gives each <DELIVERY> element its own subrecordset.

How do you refer to a subrecordset in a hierarchical database? For example, how can you refer to the <DELIVERY> elements in each <RECORD> element? You do that by referring to a new recordset, RECORD.DELIVERY . This expression refers to the child recordset made up of the <DELIVERY> elements in the current record.

As usual, this is made easier to understand with an example, so take a look at this code, where I'm binding ch08_09.xml to a table and displaying the <DELIVERY> records for each customer using tables. I start by binding a table to an XML data island and displaying the name of each customer, like this:

 <HTML>      <HEAD>         <TITLE>             Using XML With Hierarchical Records         </TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>                 Using XML With Hierarchical Records             </H1>  <XML SRC="ch08_09.xml" ID=dsoCustomer></XML>   <TABLE DATASRC="#dsoCustomer" BORDER="1">   <TR>   <TH><DIV DATAFLD="NAME"></DIV></TH>   <TD>  .             .             . 

Next , I bind a table to the RECORD field in the current record:

 <XML SRC="ch08_09.xml" ID=dsoCustomer></XML>              <TABLE DATASRC="#dsoCustomer" BORDER="1">                 <TR>                     <TH><DIV DATAFLD="NAME"></DIV></TH>                     <TD>  <TABLE DATASRC="#dsoCustomer"   DATAFLD="RECORD">  .             .             . 

To display the data from the <DATE> and <TOTAL_COST> elements in each <DELIVERY> record, I bind one final internal table to the RECORD.DELIVERY recordset. Here's the whole code:

Listing ch08_10.html
 <HTML>     <HEAD>         <TITLE>             Using XML With Hierarchical Records         </TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>                 Using XML With Hierarchical Records             </H1>             <XML SRC="ch08_09.xml" ID=dsoCustomer></XML>             <TABLE DATASRC="#dsoCustomer" BORDER="1">                 <TR>                     <TH><DIV DATAFLD="NAME"></DIV></TH>                     <TD>                         <TABLE DATASRC="#dsoCustomer"                         DATAFLD="RECORD">                             <TR>                                 <TD>  <TABLE DATASRC="#dsoCustomer"   CELLPADDING = "5"   DATAFLD="RECORD.DELIVERY">   <TR ALIGN = "LEFT">   <TH>Date</TH>   <TH>Total Cost</TH>   </TR>   <TR ALIGN = "LEFT">   <TD><DIV DATAFLD="DATE">   </DIV></TD>   <TD><DIV   DATAFLD="TOTAL_COST">   </DIV></TD>   </TR>   </TABLE>  </TD>                             </TR>                         </TABLE>                     </TD>                 </TR>             </TABLE>         </CENTER>     </BODY> </HTML> 

This page appears in Internet Explorer in Figure 8-7. As you can see there, each customer's name is displayed next to the dates and costs of the deliveries. Now you're handling hierarchical recordsets and XML.

Figure 8-7. Displaying hierarchical recordsets in Internet Explorer.

graphics/08fig07.gif



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