codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:



Home » XML » Article

Using the XML Data Source Object

Article by: Premshree Pillai (3/23/2003)
Bookmark us now! Add to Favourites
Email a friend!Tell a friend
Summary: The XML Data Source Object (DSO) is a Microsoft ActiveX control 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 the HTML file into a HTML page. In this article, I'll explain how to include the XML DSO, extract content from an external XML file, extract XML data from XML data embedded in the webpage and manipulation using JavaScript
Viewed: 62324 times Rating (25 votes): 
 4 out of 5
 Rate this Article  Read Comments  Post Comments

Using the XML Data Source Object



Download Examples (4KB)

Introduction:

The XML Data Source Object (DSO) is a Microsoft ActiveX control 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 the HTML file into a HTML page. In this article, I'll explain how to include the XML DSO, extract content from an external XML file, extract XML data from XML data embedded in the webpage and manipulation using JavaScript.

Implementation:

Initializing an XML-DSO object is done 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. Thus we initialize this control in a webpage as follows:
<OBJECT ID="SomeID" CLASSID="CLSID:550dda30-0541-11d2-9ca9-0060b0ec3d39">
</OBJECT>
Most OBJECT's have a number of parameters associated with them but the XML-DSO does not require any parameters as such.

Now we can either extract data from an external XML file or using XML Data Islands, where we embed XML code in the HTML page itself. We'll take a look at these in the examples.

Examples:

First, we'll take a look at how to extract data from XML data islands (XML data 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 any XML-DSO object. Thus, when you use a XML data island, the object is implicitly created.

In the above code, we have included a 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 the HTML tags like <A>, <SPAN>, <DIV> etc. As you can see, we have extracted data 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 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 using the above method we can extract only the first instances of these tags. To extract all instances, this can be done using 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:
Name Sex
Premshree Pillai male
Vinod male
Here, we have used the <TABLE> tag and extract 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 dispaly all the names and ages in a formatted way.

Now, we'll take a look at how to extract contents from an external XML file using XML-DSO. Consider the following XML file:
<!-- example3.xml -->
<?xml version="1.0" ?>
<ticker>
	<item>
		<message>JavaScript Ticker using XML DSO</message>
		<URL>http://someURL.com</URL>
	</item>
</ticker>
Now, consider the following HTML page:
<!-- example3.htm -->
<html>
<head>
<title>XML DSO-example3.htm</title>
<script language="JavaScript">
function load() {
	var xmlDso=myXML.XMLDocument;
	xmlDso.load("example3.xml");
}
</script>
</head>
<body bgcolor="#FFFFFF" onLoad="load()">

<object id="myXML" CLASSID="clsid:550dda30-0541-11d2-9ca9-0060b0ec3d39" width="0" height="0">
</object> <table datasrc="#myXML" border="1"> <thead> <th>Message</th> <th>URL</th> </thead> <tr> <td><div datafld="message"></div></td> <td><div datafld="URL"></div></td> </tr> </table> </body> </html>
The ouput of the above is:
Message URL
JavaScript Ticker using XML DSO http://someURL.com
Observe the code of example3.htm. First we have created a XML-DSO object with id as myXML. Note that we have added the width and height attributes to the <OBJECT> tag and set their values to 0. This is because we just want to create a XML-DSO object but do not want it to occupy any space in the webpage. Next, we have created a table with datasrc as myXML, similar to example2. We have extracted the content using <DIV> tags (within TD tags) with datafld as message for first column and URL for second column. The additional code here is the <SCRIPT> we have added. As you can see in the script, we have set the variable xmlDso to myXML.XMLDocument which refers to the object we have created. Next, we load example3.xml using the XML-DSO's load() method. Thus, now the file example3.xml is bound to the object, myXML. Everything else is similar to the previous examples.

Thus, when we want to load an external XML file using XML-DSO, we have to explicitly include the object and also use a small bit of JavaScript to load the external XML file. The above script is very specific and cannot be used to load just any XML file. A more generic script is as follows:
<script language="JavaScript">
var xmlDso;
function load(xmlFile, objName) {
	eval('xmlDso='+objName+'.XMLDocument');
	xmlDso.load(xmlFile);
}
</script>
Now, to load any XML file use:
load("SomeXMLFile.xml","anyXmlDsoObject");

XML DSO and JavaScript:

It is possible to manipulate the XML DSO object using JavaScript. Consider the following XML file:
<!-- example4.xml -->
<?xml version="1.0" ?>
<myDB>
	<member>
		<name>Premshree Pillai</name>
		<sex>male</sex>
	</member>
	<member>
		<name>Vinod</name>
		<sex>male</sex>
	</member>
	<member>
		<name>Santhosh</name>
		<sex>male</sex>
	</member>
</myDB>
Now, consider the following HTML file:
<!-- example4.xml -->
<html>
<head>
<title>XML DSO-example4.htm</title>
<script language="JavaScript">
function load() {
	var xmlDso=myDB.XMLDocument;
	xmlDso.load("example4.xml");

	/* Get the complete record set */
	var memberSet=myDB.recordset;

	/* Go to next data */
	memberSet.moveNext();
}
</script>
</head>
<body bgcolor="#FFFFFF" onLoad="load()">

<object id="myDB" CLASSID="clsid:550dda30-0541-11d2-9ca9-0060b0ec3d39" width="0" height="0">
</object> <span datasrc="#myDB" datafld="name"></span> </body> </html>
Now, the output of the above will be:
Vinod
The above script is self explanatory: Initially we store the entire data of the data file into a variable memberSet using the recordset method. The moveNext() method points to the next data item (next row). Some of other methods that can be used are :
  • movePrevious(): Point to previous data item.

  • moveFirst(): Point to first data item.

  • moveLast(): Point to last data item.

  • EOF: This property is used to check if we have reached the end of the data.

Note that, in the above methods, the data is pointed relative to parent of the nodes being displayed.

XML Ticker using XML DSO:

So far we have considered simple examples. Now, we will consider a more dynamic example, "XML Ticker using XML DSO". In this example, the ticker reads it's messages and URLs from an external example file and ticks the messages with the specified delay.
<!-- ticker.xml -->
<?xml version="1.0" ?>
<ticker>
	</item>
	<item>
		<message>JavaScripts by Premshree Pillai</message>
		<URL>http://premshree.resource-locator.com/javascripts.htm</URL>
	</item>
	<item>
		<message>The Scripting Newsletter</message>
		<URL>http://premshree.resource-locator.com/cgi-bin/newsletter.pl</URL>
	</item>
</ticker>
<!-- ticker.css -->
.tickerStyle {
	font-family:verdana,arial,helvetica; font-size:10pt; color:#666666; 
border:#666666 solid 1px; width:400px; height:20px; text-decoration:none; text-align:center;
background:#FFFFFF } .tickerStyle:hover { font-family:verdana,arial,helvetica; font-size:10pt; color:#FF6600;
border:#666666 solid 1px; width:400px; height:20px; text-decoration:none; text-align:center;
background:#FFFFFF }
<!-- ticker.htm -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>XML Ticker using XML DSO</title>
<link rel="stylesheet" href="ticker.css">

<!-- begin script -->
<script language="JavaScript">
//////////////////////////////////////////////////
//	XML Ticker using XML DSO		//
// 	(c) 2003 Premshree Pillai		//
//	Created : 19/03/03 (dd/mm/yy)		//
//	http://www.qiksearch.com		//
//	http://premshree.resource-locator.com	//
//////////////////////////////////////////////////

var xmlDso, tickerSet;
function initTicker(xmlFile, objName, counter, maxMsgs, timeOut) {
	/* Check for IE4+ */
	if(document.all&&navigator.userAgent.indexOf("Opera")==-1) {
		eval('xmlDso='+objName+'.XMLDocument');
		xmlDso.load(xmlFile);
		setTimeout("xmlDsoTicker('"+objName+"','"+counter+"','"+maxMsgs+"',
'"+timeOut+"')", timeOut); } else { alert('This Ticker works with IE4+ only!'); return false; } } function xmlDsoTicker(objName, counter, maxMsgs, timeOut) { /* Get all the data as a record set */ eval('tickerSet=' + objName + '.recordset'); if(!tickerSet.EOF && counter<maxMsgs-1) { tickerSet.MoveNext(); counter++; } else { counter=0; tickerSet.MoveFirst(); } setTimeout("xmlDsoTicker('"+objName+"','"+counter+"','"+maxMsgs+"',
'"+timeOut+"')", timeOut); } </script> <!-- end script --> </head> <body bgcolor="#FFFFFF"> <!-- begin ticker placement --> <center> <object id="ticker" CLASSID="clsid:550dda30-0541-11d2-9ca9-0060b0ec3d39" width="0"
height="0"></object> <a href="" datasrc="#ticker" datafld="URL" class="tickerStyle"> <span datasrc="#ticker" datafld="message"></span> </a> <script language="JavaScript"> var tickerMaxMsgs=2; // Maximum Messages in the XML Data file var tickerCount=tickerMaxMsgs; new initTicker("ticker.xml","ticker",tickerCount,tickerMaxMsgs,2000); </script> </center> <!-- end ticker placement --> </body> </html>
Here is a Screenshot of how the output looks like:



Observe the above codes. They are self explanatory.

I hope you have found this article useful. You may mail me your comments/suggestions.

© 2002 Premshree Pillai.
Website: http://www.qiksearch.com, http://premshree.resource-locator.com





CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'Using the XML Data Source Object '
RELATED ARTICLES
XML and JavaScript Tutorial
by Premshree Pillai
This tutorial shows how we can use XML and client side JavaScript. We will see how we can display the contents of a XML file using JavaScript, accessing child elements, manipulating elements etc.
Display XML in tree format using Javascript
by Premshree Pillai
In this article, I present a XML based client-side JavaScript that reads data from an external XML file, traverses the XML data and displays the same in a tree format. I'll use the XMLDOM ActiveX object built into Microsoft Internet Explorer for the same.
Pulling Data from Other Web pages with XMLHTTP
by Jeff Anderson
Using the little known (as yet) XMLHTTP object, part of the XML DOM Model, you can pull data from other web pages for manipulation or your own purposes.
Using the XML Data Source Object
by Premshree Pillai
The XML Data Source Object (DSO) is a Microsoft ActiveX control 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 the HTML file into a HTML page. In this article, I'll explain how to include the XML DSO, extract content from an external XML file, extract XML data from XML data embedded in the webpage and manipulation using JavaScript
XML Converter for MySQL database
by Rustem
MySQL has no native facilities for dealing with XML. This does not mean we are left out of the XML evolution. RustemSoft XML Converter has MySQL to XML support.
Creating An XML Newsfeed
by Jeff Anderson
A full explanation of how to take an XML newsfeed from moreover, amazon affiliates, google or any of the other current feed providers, and turn it into a display page using a little ASP.
XML Ticker using XML Data Source Object (DSO)
by Premshree Pillai
This news ticker shows an example of data binding using the XML DSO which is an ActiveX control built into Microsoft Internet Explorer.
Amazon Lite Web Service
by kokogiak.com
A complete XML web service allowing you to offer complete Amazon searches and disaply results on your site. A fantastic introduction to XML Web Services.
XML Ticker
by Premshree Pillai
This is an XML based JavaScript Ticker that can tick any number of messages. The ticker works with IE only. The ticker reads it's contents, i.e the ticker style, text to be displayed, the link for that particular message from a XML file.
Batch processing XML with XSLT 2.0
by Krunal Patel
What you need is an XML version of the directory listing. Then, you could use that XML file as the single input file to XSLT and process each file using XSLT. It would be wonderful if you could do the directory processing in XSLT directly. Unfortunately, with all the power of XSLT -- and particularly XSLT 2.0 -- the language still doesn't have directory operations.








Recent Forum Threads
• SoftIce Driver Suite 3.0.1
• metrowerks codewarrior 9.0
• Mapinfo.Professional.v7.0
• GardenGraphics DynaSCAPE Professional 3.02
• Embroidery Great Notions 2004 part01-74
• EFI_BEST_COLORPROOF_V5.0
• AViD.SOFTiMAGE.XSi.V4.2.CD1-NNiSO
• AUTOFX.PHOTOGRAPHIC.EDGES.6.0.CD1
• 0 STI.SASSPro.v2.0.0.2 working full


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2014