Sams Teach Yourself XML in 21 Days
- Table of Contents
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Introduction
- Part I: At a Glance
- Day 1. Welcome to XML
- Day 2. Creating XML Documents
- Day 3. Creating Well-Formed XML Documents
- Day 4. Creating Valid XML Documents: DTDs
- Declaring Attributes in DTDs
- Day 6. Creating Valid XML Documents: XML Schemas
- Day 7. Creating Types in XML Schemas
- Part I. In Review
- Day 8. Formatting XML by Using Cascading Style Sheets
- Day 9. Formatting XML by Using XSLT
- Day 10. Working with XSL Formatting Objects
- Part II. In Review
- Part III: At a Glance
- Day 11. Extending HTML with XHTML
- Day 12. Putting XHTML to Work
- Day 13. Creating Graphics and Multimedia: SVG and SMIL
- Day 14. Handling XLinks, XPointers, and XForms
- Part III. In Review
- Part IV: At a Glance
- Day 15. Using JavaScript and XML
- Day 16. Using Java and .NET: DOM
- Day 17. Using Java and .NET: SAX
- Day 18. Working with SOAP and RDF
- Part IV. In Review
- Part V: At a Glance
- Day 19. Handling XML Data Binding
- Introducing DSOs
- Binding HTML Elements to HTML Data
- Binding HTML Elements to XML Data
- Binding HTML Tables to XML Data
- Accessing Individual Data Fields
- Binding HTML Elements to XML Data by Using the XML DSO
- Binding HTML Tables to XML Data by Using the XML DSO
- Searching XML Data by Using a DSO and JavaScript
- Handling Hierarchical XML Data
- Summary
- Q&A
- Workshop
- Day 20. Working with XML and Databases
- Day 21. Handling XML in .NET
- Part V. In Review
- Appendix A. Quiz Answers
Binding HTML Elements to XML Data
To bind XML data to controls in Internet Explorer, use your standard XML document that contains the state data, which is shown in Listing 19.3. (Note that we've removed the units attribute from the <population> element because Internet Explorer can't convert that element's data into readable form when XML elements use attributes.)
Example 19.3. An XML Data Document (ch19_03.xml)
<?xml version="1.0" encoding ="UTF-8"?>
<states>
<state>
<name>California</name>
<population>33871648</population>
<capital>Sacramento</capital>
<bird>Quail</bird>
<flower>Golden Poppy</flower>
<area units="square miles">155959</area>
</state>
<state>
<name>Massachusetts</name>
<population>6349097</population>
<capital>Boston</capital>
<bird>Chickadee</bird>
<flower>Mayflower</flower>
<area units="square miles">7840</area>
</state>
<state>
<name>New York</name>
<population>18976457</population>
<capital>Albany</capital>
<bird>Bluebird</bird>
<flower>Rose</flower>
<area units="square miles">47214</area>
</state>
</states>
You'll start by using XML data islands to bind to the data in ch19_03.xml. All you need to do is change the DSO you're using to an XML island by using the <XML> element, as shown in Listing 19.4.
Example 19.4. Reading XML Data with XML Islands (ch19_04.html)
<HTML>
<HEAD>
<TITLE>
Binding with XML data islands
</TITLE>
</HEAD>
<XML SRC="ch19_03.xml" ID="states"></XML>
<BODY>
<H1>
Binding with XML data islands
</H1>
Name: <INPUT TYPE="TEXT" DATASRC="#states"
DATAFLD="name" SIZE="10">
<BR><BR>
Population: <INPUT TYPE="TEXT" DATASRC="#states"
DATAFLD="population" SIZE="10">
<BR><BR>
Capital: <SPAN DATASRC="#states"
DATAFLD="capital"></SPAN>
<BR><BR>
Bird: <SELECT DATASRC="#states"
DATAFLD="bird" SIZE="1">
<OPTION VALUE="Quail">Quail
<OPTION VALUE="Chickadee">Chickadee
<OPTION VALUE="Bluebird">Bluebird
</SELECT>
<BR><BR>
Flower: <SPAN DATASRC="#states" DATAFLD="flower">
</SPAN>
<BR><BR>
<BUTTON ONCLICK=
"states.recordset.moveFirst()"> <<
</BUTTON>
<BUTTON ONCLICK="if (!states.recordset.BOF)
states.recordset.movePrevious()"> <
</BUTTON>
<BUTTON ONCLICK="if (!states.recordset.EOF)
states.recordset.moveNext()"> >
</BUTTON>
<BUTTON ONCLICK=
"states.recordset.moveLast()"> >>
</BUTTON>
</BODY>
</HTML>
The results are the same as when you used the MSHTML DSO and HTML data, as shown in Figure 19.2.
Figure 19.2 Data binding with XML data islands.
Now you have a way to display XML data in HTML controls in Internet Explorer.
Binding HTML Tables to XML Data | Next Section

Account Sign In
View your cart