350 likes | 518 Views
TUTORIAL 9. USING XML AS A DATA SOURCE. USING XML AS A DATA SOURCE. Data binding is a process by which information in a data source is stored as an object in computer memory. In this tutorial, the data source is an XML document containing information about the employees at Freezing Point.
E N D
TUTORIAL 9 USING XML AS A DATA SOURCE New Perspectives on XML, 2nd Edition Tutorial 9
USING XML AS A DATA SOURCE • Data binding is a process by which information in a data source is stored as an object in computer memory. • In this tutorial, the data source is an XML document containing information about the employees at Freezing Point. • The Web pages uses placeholders which we will later populate with data from two XML documents. New Perspectives on XML, 2nd Edition Tutorial 9
USING XML AS A DATA SOURCE New Perspectives on XML, 2nd Edition Tutorial 9
DATA BINDING WITH SEVERAL WEB PAGES New Perspectives on XML, 2nd Edition Tutorial 9
FIELDS, RECORDS, AND RECORDSETS • Data in a data source is organized by fields, records, and recordsets. • A field is an element that contains a single item of information such as an employees last name. • A record is a collection of those fields. • A recordset is a collection of records. New Perspectives on XML, 2nd Edition Tutorial 9
FIELDS, RECORDS, AND RECORDSETS This figure shows fields, records, and a recordset of an XML document New Perspectives on XML, 2nd Edition Tutorial 9
DATA ISLANDS • The first step in data binding is to attach the Web page to a recordset. The attached data is called a data island. They can be either external files or code entered into the HTML file. • The syntax to create a data island from an external file is: <xml id=“id” src=“URL”></xml> • Here, id is the id name assigned to the data island • URL is the filename and location of the external XML file New Perspectives on XML, 2nd Edition Tutorial 9
DATA ISLANDS • For example: <xml id=“Company” src=“Company.xml”></xml> • This creates a data island named Company attached to Company.xml. New Perspectives on XML, 2nd Edition Tutorial 9
DATA ISLANDS • To insert a data island directly into the HTML file, use this syntax: <xml id=“id”> xml code </xml> • While this technique can be used, it is not recommended. After all, the philosophy of XML is to separate data content from data formatting. New Perspectives on XML, 2nd Edition Tutorial 9
DATA ISLANDS • Data islands are stored by the XML parser as a Data Source Object (DSO). • The DSO takes care of the interaction between the Web page and the data island. Also, program code can be written to control the actions of the DSO such as specifying which records will be displayed in the Web page at any one time. New Perspectives on XML, 2nd Edition Tutorial 9
CREATING A DATA ISLAND New Perspectives on XML, 2nd Edition Tutorial 9
BINDING AN HTML ELEMENT TO A FIELD • After the data island has been created, the elements in the XML document need to be bound to the HTML file. • The syntax is: <tag datasrc=“#id” datafld=“field”> • Here, tag is the name of the HTML tag, id is the name of the data island, and field is the name of the field in the data source. New Perspectives on XML, 2nd Edition Tutorial 9
HTML ELEMENTS THAT SUPPORT DATA BINDING New Perspectives on XML, 2nd Edition Tutorial 9
BINDING AN HTML ELEMENT TO A FIELD New Perspectives on XML, 2nd Edition Tutorial 9
BINDING TO AN XML ATTRIBUTE • Attributes, like the Status attribute of the Employee element, are treated by the DSO as fields. If the attribute is part of a record element, it is easy to bind attribute values to a Web page. New Perspectives on XML, 2nd Edition Tutorial 9
BINDING TO AN XML ATTRIBUTE • This code has the ID attribute as part of the Employee element: <employee ID=“E304”> <name>Alice Ashman</name> <deptName>Accounting</deptName> </employee> New Perspectives on XML, 2nd Edition Tutorial 9
BINDING TO AN XML ATTRIBUTE • And it is interpreted by the DSO as: <employee> <ID>E304</ID> <name>Alice Ashman</name> <deptName>Accounting</deptName> </employee • If the attribute is part of a field element, it is still treated by the DSO as a field element. New Perspectives on XML, 2nd Edition Tutorial 9
BINDING TO AN XML ATTRIBUTE • The field element containing the attribute becomes a record element. • Remember to reference all character data within an element using the $TEXT field. • It is a good idea not to use attributes in field elements if you plan to do data binding. New Perspectives on XML, 2nd Edition Tutorial 9
BINDING TO AN XML ATTRIBUTE New Perspectives on XML, 2nd Edition Tutorial 9
THE DATA SOURCE OBJECT • ActiveX Data Objects (ADO) is a data-access technology developed by Microsoft. ADO allows you to work with the Data Source Object by applying a method or by changing one of the properties of the DSO. • The syntax for applying a method is: id.recordset.method() New Perspectives on XML, 2nd Edition Tutorial 9
THE DATA SOURCE OBJECT • Here, id is the name of the data island in the Web document and method is the name of the method supported by ADO. • There are several methods that can be applied to DSOs. New Perspectives on XML, 2nd Edition Tutorial 9
THE DATA SOURCE OBJECT New Perspectives on XML, 2nd Edition Tutorial 9
THE DATA SOURCE OBJECT • For example, if you want to display the last record in a DSO whose id is “staffInfo”, run the following method: staffInfo.recordset.moveLast( ) • The simplest way to run a method is to assign the method to the onClick event handler of a <button> as shown below: <button onClick=“staffInfo.recordset.moveLast( )”> New Perspectives on XML, 2nd Edition Tutorial 9
THE DATA SOURCE OBJECT • When the user clicks the button, the browser runs the command indicated by the onClick event handler, displaying the last record. New Perspectives on XML, 2nd Edition Tutorial 9
ASSIGNING A RECORDSET METHOD New Perspectives on XML, 2nd Edition Tutorial 9
TABLE BINDING • Using table data binding, each record can be displayed in a different row of a table. The syntax is: <table datasrc=“#id”> <tr> <td><span datafld=“field1”></span></td> <td><span datafld=“field2”></span></td> </tr> </table> New Perspectives on XML, 2nd Edition Tutorial 9
TABLE BINDING • In the example, id is the name of the data island, field1, field2 are the fields from the recordset. New Perspectives on XML, 2nd Edition Tutorial 9
TABLE PAGES • As you add more records to your XML document, a table can become long and unwieldy. One way to fix this is to give the user the option of limiting the number of records displayed at any one time. • The user can then move forward of backward that number of records at a time. This is called paging. New Perspectives on XML, 2nd Edition Tutorial 9
TABLE PAGES • To specify the page size, add the dataPageSize attribute to the <table> tag: datapagesize=“number” • number is the number of records you want displayed in a single page. New Perspectives on XML, 2nd Edition Tutorial 9
NAVIGATING A TABLE PAGE • A unique identifier must be assigned to a table using the ID attribute before writing a command to navigate a table page. The syntax to do this is: <table id=“id”> • Here, id is the name you assign to the table object. • This is needed because the commands to navigate the table pages act on the table itself not the recordset. New Perspectives on XML, 2nd Edition Tutorial 9
TABLE METHODS AND PROPERTIES New Perspectives on XML, 2nd Edition Tutorial 9
TABLE METHODS AND PROPERTIES • To run these commands, add the command to the onClick event handler of a <button> tag. For example, to move to the last page in a data table named “StaffTable”, you enter the attribute: onClick=“staffTable.lastPage( )” New Perspectives on XML, 2nd Edition Tutorial 9
HIERARCHICAL RECORDSETS New Perspectives on XML, 2nd Edition Tutorial 9
HIERARCHICAL RECORDSETS • To bind the Employee fields in the previous slide to a table, you create a table as follows: <table datasrc=“#staffInfo” datafld=“employee”> <tr> <td><span datafld=“name”></span></td> <td><span datafld=“position”></span></td> <td><span datafld=“phone”></span></td> … </tr> </table> New Perspectives on XML, 2nd Edition Tutorial 9
THE FINAL WEB PAGE New Perspectives on XML, 2nd Edition Tutorial 9