IEMobile Team Weblog

Information about Internet Explorer for Windows Mobile

XML Data Islands in IE Mobile

I am Steve Meredith, a developer on the IE Mobile team. I want to continue the blog theme of XML support in IE Mobile. In Windows Mobile 5, we support MSXML3. Prior to then, we supported MSXML2.

 

An XML data island is simply an XML data set embedded in an HTML document. The browser doesn’t directly render the XML data, but the elements can be accessed via script. You can use this technique to create dynamic web pages that don’t have to make requests to the server for data.

 

To demonstrate data islands, let’s create a web page that lets you select a food from two dropdown lists. The first list contains a category of food: fruit, vegetables, or nuts. The second list contains foods appropriate to the currently selected category. So if “fruit” is selected, the second list contains kinds of fruit. If “vegetables” is selected, the second list contains kinds of vegetables. We don’t want the browser to request a new page from the server each time the category is changed.

 

Below is the HTML used to generate the page in Figure 1. The script has been removed for clarity. I will explain it later.

 

<html>

<head>

<title>IE Mobile XML Data Island Example</title>

</head>

 

<body onload='loadCategoryList(); loadFoodList()'>

 

<!-- xml data island: 3 categories for dropdown 1, each with different foods for dropdown 2. -->

<xml id='myFoods'>

    <categories>

        <category name='Fruit'>

            <food>Apples</food>

            <food>Bananas</food>

            <food>Strawberries</food>

        </category>

        <category name='Vegetables'>

            <food>Peas</food>

            <food>Carrots</food>

            <food>Corn</food>

        </category>

        <category name='Nuts'>

            <food>Almonds</food>

            <food>Walnuts</food>

            <food>Pecans</food>

            <food>Pistachios</food>

        </category>

    </categories>

</xml>

 

<!-- You could also put the XML into a seperate file and use src.

<xml id='myFoods' src='myFoods.xml' />

-->

 

<script type="text/javascript">

<!-- function loadCategoryList() -->

<!-- function loadFoodList() -->

</script>

 

<p>

This sample shows how to use script and an XML data island to change the values of a second drop-down based on the value of the first.

</p>

<hr />

Select from the available foods:

 

<form id='myForm'>

    <select id='categoryList' onchange='loadFoodList()'></select>

    <select id='foodList'></select>

</form>

 

</body>

</html>

 

The key to this technique is the <xml> element. You enclose your XML document between <xml> and </xml>, then use an XMLDocument property to access it via script.

 

The <xml> element supports the src attribute and the id attribute. If it is more appropriate for your application, you can put the XML in a separate file, and load it into the data island using the src attribute, as follows:

 

<xml id='myFoods' src='myFoods.xml' />

 

To access the XML data from script, use the XML DOM. You can access it using the XMLDocument property of the <xml> element. For example, since the <xml> element in our example above has the id “myFoods”, you can get the XML DOM like this:

 

xmlDoc = myFoods.XMLDocument;

 

Below is the function to load the first list, categories, with the values from the XML. This function is only called once: it is an onload event handler for the <body> element. We start by using XMLDocument to get the XML DOM for our data. Then, we loop through each <category> element and add the name from the “name” attribute to the category list using the Option object.

 

// Initialize the first dropdown (categories) from the XML.

function loadCategoryList()

{

    // Get the XML DOM for the data island.

    xmlDoc = myFoods.XMLDocument;

 

    // Get all the categories.

    categories = xmlDoc.getElementsByTagName('category');

    for (g = 0; g < categories.length; g++)

    {

        // Add the category to the <select> element.

        option = new Option();

        option.text

           = categories[g].attributes.getNamedItem('name').text;

        option.value = option.text;

        document.all.categoryList.options.add(option);

    }

}

 

Below is the function to load the second list, the foods. This function is called when the body is loaded and every time a new category is selected. First, we find out what is selected in the categories list. Then, we get the XML DOM and search for that category. Once we find it, we use the foods in that category to load up the foods list, again using the Option object for loading the <select> element.

 

// Initialize the second dropdown (foods) from the

// XML based on the value of the first dropdown (categories).

function loadFoodList()

{

    // Find the name of the selected category in the 1st box.

    selectedIndex = document.all.categoryList.options.selectedIndex;

    categoryName = document.all.categoryList.options[selectedIndex].text;

 

    // Get the XML DOM for the data island.

    xmlDoc = myFoods.XMLDocument;

 

    // Search the XML for the selected category.

    categories = xmlDoc.getElementsByTagName('category');

    for (g = 0; g < categories.length; g++)

    {

        // Found the selected category.

        if (categoryName

                == categories[g].attributes.getNamedItem('name').text)

        {

            // Delete any current option elements.

            document.all.foodList.length = 0;

 

            // Fill the select element with food from this category.

            foods = categories[g].getElementsByTagName('food');

            for (i = 0; i < foods.length; i++)

            {

                option = new Option();

                option.text = foods[i].text;

                option.value = foods[i].text;

                document.all.foodList.options.add(option);

            }

        }

    }   

}

 

There are other solutions to this particular dropdown problem, but hopefully this example demonstrates the use of the <xml> element, the XMLDocument property, and how to use the XML DOM to access the XML.

 

You can access the complete source file here: <http://iemobile.members.winisp.net/DataIsland/sample.htm>.

 

Steve Meredith

IE Mobile

 

Published Thursday, March 16, 2006 4:06 PM by iemoblog

Attachment(s): Figure1.JPG

Comments

 

Varouj Chitilian said:

The ability to create HTML element Objects and add them to the html DOM (ie. new Option and Options.add) seems to be new since Pocket IE 2003, can you comment?  Are these methods specific to Options or will they be exposed for other HTML Elements and collections as well?  When will you guys support DOM Level1?

Also will you be creating MSDN documentation similar to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/apippc/html/ppc__html_reference_for_pocket_internet_explorer.asp for IE Mobile?

Keep up the blogging, I find it very useful.

Thanks,
Varouj
March 16, 2006 7:33 PM
 

Jesus said:

What?  You're just sticking XML in the middle of a HTML document with no namespace?  Could you explain how that makes any sense at all?
April 28, 2006 12:07 PM
 

Zandoná Mobile® said:

I have been asked about AJAX development support for Internet Explorer Mobile (running on Windows Mobile-based...
May 3, 2006 10:29 AM
 

IEMobile Team Weblog said:

This is Steve Meredith again. Last time, I wrote about XML Data Island support in IE Mobile. The &amp;lt;xml&amp;gt;...
May 3, 2006 1:53 PM
 

Ruslan Trifonov's blog said:

Having my Vista gadget from my previous posts( 1 , 2 ), I decided to see if I&#39;ll be able to monitor

October 28, 2006 6:33 PM
 

Richard Jones - Mobile LOB Blog - Its Mobile AJAX Wednesday... said:

April 18, 2007 5:02 PM
 

Healthy Food » IEMobile Team Weblog : XML Data Islands in IE Mobile said:

December 17, 2007 5:40 AM
 

IEMobile Team Weblog XML Data Islands in IE Mobile | Indoor Grills said:

June 1, 2009 3:57 AM
 

IEMobile Team Weblog XML Data Islands in IE Mobile | Portable Greenhouse said:

June 1, 2009 7:29 AM
 

IEMobile Team Weblog XML Data Islands in IE Mobile | Cellulite Creams said:

June 8, 2009 10:32 PM
 

IEMobile Team Weblog XML Data Islands in IE Mobile | Cast Iron Cookware said:

June 11, 2009 10:34 PM
 

IEMobile Team Weblog XML Data Islands in IE Mobile | bar stools said:

June 14, 2009 1:47 AM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker