Article

Home » Client-side Coding » XML, XSLT & Web Services » Using the XML Data Source Object

About the Author

Premshree Pillai

author_premshree Premshree studies engineering in Information Technology at Mumbai University, India. He's written articles for a range of popular Indian IT magazines, and his site, QikSearch has featured in 'Digit' Magazine - India's No. 1 technology magazine.

View all articles by Premshree Pillai...

Using the XML Data Source Object

By Premshree Pillai

May 9th, 2003

Reader Rating: 8

Page: 1 2 3 Next

The XML Data Source Object (DSO) is a Microsoft ActiveX control that’s built into Microsoft Internet Explorer 4+. Using this object, it is possible to extract content from an external XML file, or XML data embedded in an HTML file, into an HTML page.

In this article, I'll explain how to include the XML DSO, extract content from an external XML file, extract XML data that’s embedded in a Web page and manipulate that data using JavaScript.

To work through these examples, you'll need this zip, which contains the sample code.

Implementation

We can initialize an XML-DSO object using the <OBJECT> tag. The CLASSID for the XML-DSO is:

CLSID:550dda30-0541-11d2-9ca9-0060b0ec3d39

The above ID uniquely identifies the XML-DSO. And we initialize this control in a Web page as follows:

<OBJECT ID="SomeID" CLASSID="CLSID:550dda30-0541-
11d2-9ca9-0060b0ec3d39"></OBJECT>

Most OBJECTs have a number of parameters associated with them, but the XML-DSO does not require any such parameters.

Now, we can either extract the data from an external XML file, or, we can use XML Data Islands, where we embed XML code in the HTML page itself. Let’s take a look at both these solutions.

Examples

First, we'll take a look at how to extract data from XML data islands (XML data that’s included in the HTML page itself). Take a look at the following code:

<!-- example1.htm -->
<html>
<head>
<title>XML DSO-example1.htm</title>
</head>
<body bgcolor="#FFFFFF">

<xml id="xmldb">
 <db>
   <member>
     <name>Premshree Pillai<name>
     <sex>male</sex>
   </member>
   <member>
     <name>Vinod</name>
     <sex>male</sex>
   </member>
 </db>
</xml>

<span datasrc="#xmldb" datafld="name"<</span>
<br>
<span datasrc="#xmldb" datafld="sex"></span>

</body>
</html>

The output of the above is:

Premshree Pillai
male

Note that, in the code for example1.htm, we have not initialised an XML-DSO object. Thus, when you use a XML data island, the object is implicitly created.

In the above code, we’ve included an XML data island using the <XML> tag. We have assigned it an ID, xmldb, for use later. Now, we can extract data from the XML data island using HTML tags like <A>, <SPAN>, <DIV> etc. As you can see, we have extracted data here using the <SPAN> tag. Note the attributes datasrc and datafld in the <SPAN> tag. datasrc is used to specify the ID of the data island you want to extract data from. datafld is used to specify the XML tag you want to extract the data from (here, name in first <SPAN> and sex in second <SPAN>).

Note that we have two <name> and <sex> tags in our XML data island, but that, using the above method, we can extract only the first instances of these tags. To extract all instances, we have to use the <TABLE> tag. Take a look at the following example:

<!-- example2.htm -->
<html>
<head>
<title>XML DSO-example2.htm</title>
</head>
<body bgcolor="#FFFFFF">

<xml id="xmldb">
 <db>
   <member>
     <name>Premshree Pillai<name>
     <sex>male</sex>
   </member>
   <member>
     <name>Vinod</name>
     <sex>male</sex>
   </member>
 </db>
</xml>

<table datasrc="#xmldb" border="1">
 <thead>
   <th>Name</th>
   <th>Sex</th>
 </thead>
 <tr>
   <td><div datafld="name"></div></td>
   <td><div datafld="sex"></div></td>
 </tr>
</table>

</body>
</html>

The output of the above is:

1121_image1

Here, we’ve used the <TABLE> tag and extracted the contents using the HTML tag, <DIV>, within the HTML column tag, <TD>. The table will automatically iterate through each instance of <member> (the parent of <name> and <sex>), thus, we can display all the names and ages in a formatted way.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article