XML provides a universal medium for delivering data, and IE 5.0+ offers the functionality to quickly create data solutions. One of these functions is the ability to bind HTML elements to data sources, specifically, XML data islands.

The XML data will reflect any change to the data in the bound element; XML data islands also provide scriptable events.

You access the data in the XML data islands through standard DOM methods, e.g., selectSingleNode(), selectNodes(), etc. This functionality makes it easy to quickly create data entry devices and reports.

An XML data island is XML surrounded by <XML> tags in a DHTML document. Here's an example of an XML data island:

<XML ID="xmlTest" NAME="xmlTest">
<root>
<data>
<cat>Whiskers</cat>
<dog>Spot</dog>
<fish>Bubbles</fish>
</data>
</root>
</XML>

In order to bind this data to HTML elements, you must specify the DATASRC and DATAFLD attributes on those elements. The following code demonstrates how to bind the "cat" field to an <INPUT> element:

<INPUT TYPE="text" DATASRC="#xmlTest" DATAFLD="cat" SIZE=15>

The "#" preceding the XML data island ID in the DATASRC attribute must be present in order to bind the data. Since the value of the <INPUT> is bound to the XML data source, the XML data will reflect any change to the value of the <INPUT>. When the page loads, the value of the <INPUT> will be "Whiskers". If a user types in a new name, the "text" property of the "cat" XML node will change to the new value of the <INPUT>.

Another handy feature is the ability to bind a <TABLE> to a data source. When you bind the <TABLE> to the XML data island, you simply set the DATASRC attribute of the <TABLE> to the data island ID. As long as you have a bound element in one <TR>, the bound <TABLE> will reflect all the rows in your data island. Here's an example:

<XML ID="xmlTest" NAME="xmlTest">
<root>
<dog>
<name>Spot</name>
</dog>
<dog>
<name>Rover</name>
</dog>
<dog>
<name>Jack</name>
</dog>
</root>
</XML>
<TABLE BORDER=1 DATASRC="#xmlTest">
<TR>
<TD><SPAN DATAFLD="name"></SPAN></TD>
</TR>
</TABLE>

What you should see with this code is a table with three rows, and each row will have the dogs' names included in the cell.

Let's use this information to create a simple form for submitting data to be stored in a database. This form will be for the user to add/update personal information. Here's the HTML/XML:

Somewhere on the page, you should add an <INPUT> of type "button". Then add this function to the "onclick" event:

function button_onclick() {
  document.frmSubmit.txtXML.value = xmlUser.xml;
  document.frmSubmit.submit();
}

In your parsing page, you should load the XML that's passed through in the "txtXML" request. You can then load this XML into a DOM document to parse the information. In ASP, you would load the XML like this:

Dim oDOM
oDOM = Server.CreateObject("MSXML.DOMDocument")
oDOM.loadXML Request("txtXML")

Then you would handle the XML from there. (This is outside the scope of this article.)

Once you have your data in XML, a world of opportunities and tools (both client side and server side) are open to you.

Do you need help with HTML? Gain advice from Builder AU forums

Related links

Comments

1

hila - 18/09/06

hi, this is very good article but i have xml that i can't show it in html.
I want to show person details in textbox
and his phone list in table.

can you help me please.

<?xml version="1.0" encoding="utf-8" ?> 
<Persons>
<Person>
<FName>Yair</FName>
<LName>Greenberg</LName>
<Email>yair@bla.com</Email>
<Gender>Male</Gender>
<Street>Ben 12</Street>
<City>Tel-Aviv</City>
<Phones>
<Phone>
<Num>03-4515411</Num>
<Dsc>Home</Dsc>
</Phone>
<Phone>
<Num>03-9874741</Num>
<Dsc>Office</Dsc>
</Phone>
<Phone>
<Num>054-4887474</Num>
<Dsc>Mobile</Dsc>
</Phone>
</Phones>
</Person>

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

1

hila - 18/09/06

hi, this is very good article but i have xml that i can't show it in html. I want to show person ... more

Log in


Sign up | Forgot your password?

  • Staff Microsoft shows off IE9 preview

    This week, highlights from Microsoft's MIX10 conference and more in the Roundup. Read more »

    -- posted by Staff

  • Chris Duckett IE9's H.264 vote killed Ogg

    In a split decision by the judges, the winner of the W3C/WHATWG video codec consensus is H.264, taking home the future of video playback on the internet while loser Ogg goes home with nothing but thoughts of what might have been. Read more »

    -- posted by Chris Duckett

  • Staff Google launches Apps Marketplace

    Google launches and app store, while Mozilla plans to re-write its open-source license. More of this week's news in the Roundup. Read more »

    -- posted by Staff

What's on?

  • Optus Deal

    Broadband + home phone + PlayStation®3 in a single package price!