The Properties, Methods , and Events of XML DSOsIn the example at the beginning of this chapter, we saw that you can use recordset methods such as moveFirst , moveLast , moveNext , and movePrevious to move around in a recordset. I'll make that more systematic now. In particular, the recordset object in an XML DSO has these properties:
Here are the methods of the recordset objects inside XML DSOs:
XML DSOs also have a number of events that you can handle. (Recall that we saw how to handle XML document events in Internet Explorer at the end of the previous chapter.) Here they are:
To let the user navigate around in the recordset created from ch08_03.xml, I'll add the same buttons we saw in the earlier HTML example, using methods such as customers.recordset.moveNext() to navigate, like this: Listing ch08_04.html<HTML> <HEAD> <TITLE>Single Record Binding Using XML Data Islands</TITLE> </HEAD> <XML SRC="ch08_03.xml" ID="customers"></XML> <BODY> <CENTER> <H1> Single Record Binding Using XML Data Islands </H1> Name: <INPUT TYPE="TEXT" DATASRC="#customers" DATAFLD="NAME" SIZE=10> <P> Customer ID: <INPUT TYPE="TEXT" DATASRC="#customers" DATAFLD="CUSTOMER_ID" SIZE=5> <P> Purchase date: <SPAN DATASRC="#customers" DATAFLD="PURCHASE_DATE"></SPAN><P> Product: <SPAN DATASRC="#customers" DATAFLD="PRODUCT_NAME"></SPAN> <P> Department: <SELECT DATASRC="#customers" DATAFLD="DEPARTMENT" SIZE=1> <OPTION VALUE="Meat">Meat <OPTION VALUE="Produce">Produce <OPTION VALUE="Frozen">Frozen </SELECT> <P> <BUTTON ONCLICK="customers.recordset.moveFirst()" > << </BUTTON> <BUTTON ONCLICK="if (!customers.recordset.BOF) customers.recordset.movePrevious()" > < </BUTTON> <BUTTON ONCLICK="if (!customers.recordset.EOF) customers.recordset.moveNext()" > > </BUTTON> <BUTTON ONCLICK="customers.recordset.moveLast()"> >> </BUTTON> </CENTER> </BODY> </HTML> You can see this page in Figure 8-2, where you see the fields of the current record displayed in bound HTML elements. The user can click the buttons in the page to navigate through the recordset. Figure 8-2. Using data binding to display an XML document in Internet Explorer.
As you can see in the previous list, there's a lot more you can do with a recordset than just navigate through the recordset. For example, it's often useful to access the individual fields in a record. Say that you wanted to get the value in the CUSTOMER_ID field of the current record in the DSO; you can use the expression customers.recordset("CUSTOMER_ID") to do that.
I'll access individual fields in records now in an example to make this concrete. In this case, I'll loop over all the records in the database (which I can do with a while loop, looping until the recordset's EOF property is true ) and display the customer name, the item purchased, and what department the purchase was made in. Here's what the code looks like: Listing ch08_05.html<HTML> <HEAD> <TITLE> Accessing individual data fields </TITLE> <XML ID="customer" SRC="ch08_03.xml"></XML> <SCRIPT LANGUAGE="JavaScript"> function viewData() { while (!customer.recordset.EOF) { div1.innerHTML += customer.recordset("NAME") + " bought " + customer.recordset("PRODUCT_NAME") + " from the " + customer.recordset("DEPARTMENT") + " department.<BR>" customer.recordset.moveNext() } } </SCRIPT> </HEAD> <BODY> <CENTER> <H1> Accessing individual data fields </H1> </CENTER> <FORM> <CENTER> <INPUT TYPE="BUTTON" VALUE="View data" ONCLICK="viewData()"> </CENTER> </FORM> <DIV ID="div1"> </DIV> </BODY> </HTML> You can see this page in operation in Figure 8-3. As you can see there, the data from the individual fields in the various records has been assembled and displayed. Figure 8-3. Accessing individual data fields.
Besides working with single records as we have up to this point, you can work with all the data in an XML document at once when you bind it to a table. |