Making Use of the XML Data Source Object

he XML Data Source Object (DSO) is a Microsoft ActiveX control that’s been built into Microsoft Internet Explorer since version 4. This object allows you to extract content from an external XML file or XML data embedded in the HTML file into a HTML page.

You can use the XML-DSO in a Web page to extract content from an external XML file, extract XML data from XML data embedded in the Web page, and manipulate that data using JavaScript. However, it is not advisable to use this object for the Internet since XML-DSO only works on MSIE 4+ browsers, which can raise compatibility issues. It is much more advisable to use XML-DSO for intranets.

Getting Started
To initialize the XML-DSO object, use the tag. The CLASSID for the XML-DSO is:

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

This ID uniquely identifies the XML-DSO. Initialize this control in a Web page using this code:

Though most OBJECTs have a number of parameters associated with them, the XML-DSO does not require any.

Extract Data Using an XML Data Island
First, include an XML data island by using the tag. Next, assign it an ID, xmldb?for use later. Data is actually extracted using HTML tags: , ,

, etc. The code in Listing 1 uses the tag. The datasrc attribute specifies the ID of the data island from which you want to extract data. The datafld attribute specifies the XML tag from which you want the data. So, the first extracts the name, and the second extracts the gender.

Note that the code does not initialize an XML-DSO object. This is because the use of an XML data island implicitly creates one.

The output should be:

Premshree Pillaimale

Note that there are two and tags in the XML data island. Using this method, you can only extract the first instances of these tags. The code in Listing 2 extracts all instances using the

tag:

The output should be:

Name

Sex

Premshree Pillai

male

Vinod

male

In Listing 2, the

tag extracts data using
tag within the
tag. The table will automatically iterate through each instance of (the parent of and ).

Extract Data Using an External XML File
To load an external XML file using XML-DSO, you have to explicitly include the object and use a small bit of JavaScript.

First create an XML-DSO object, with the ID myXML. Add the width and height attributes to the tag and set their values to 0. This ensures that the XML-DSO object won’t occupy any space in your Web page.

Next, create a table with datasrc as myXML?similar to Listing 2. The code extracts the data using

tags (within TD tags) using datafld as the message for the first column and the URL for the second column. The
Message URL

The output should be:

Message

URL

JavaScript Ticker using XML DSO

http://someURL.com

The above script is very specific. Here’s a more generic script:

Now, to load any XML file use: load("SomeXMLFile.xml","anyXmlDsoObject");

Use XML-DSO and JavaScript Together
Suppose you have an XML file that contains names, email addresses, and phone numbers. You want to use it to build an app that displays the records of each person&#151one at a time. Users would browse each person’s data using a “Next” and a “Previous” button. Javascript can help you do this.

The following code stores all the data in the file into a variable memberSet using the recordset method. The moveNext() method points to the next data item (next row). The script then loads the XML file example4.xml and stores the records in the variable memberSet. The first record will be displayed, but memberSet.moveNext() points to the next record in the file relative to the previously pointed data.

			Premshree Pillai		male				Vinod		male				Santhosh		male	

Here’s the corresponding HTML file:

XML DSO-example4.htm

The output should be:

Vinod

Here are more ways to manipulate the XML-DSO using JavaScript:

  • 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.

In the above methods, the data is pointed relative to the parent of the nodes being displayed.

An XML Ticker Using XML-DSO
It may be helpful to see this process working in a more dynamic example. Instead of displaying static data, Listing 3 is of a news ticker that reads messages from an XML file, then displays each one from the file at regular intervals. It also displays the URL to go to when clicked on a particular message (news), from an external example file and ticks the messages with the specified delay. Such a ticker can be used to display news items on a Web page. Updating the news will involve updating the XML file only.

Listing 3 shows how to display data from an XML file, move forward in the recordset (using moveNext()) and move backwards in the recordset (using movePrevious()).

The script in Listing 3 uses xmlDso and tickerSet as global variables. There are two functions initTicker() and xmlDsoTicker(). The initTicker() function takes the following five arguments:

  • xmlFile: The XML file to read ticker data from.
  • objName: The ID of the object we have used in the HTML file to initialize the XML-DSO.
  • counter: A dummy variable to loop through the XML data.
  • maxMsgs: The number of messages in the XML file.
  • timeOut: Delay in milliseconds

initTicker() first checks for IE 4+. If the browser is IE4+, the XML file is passed as an argument and loaded. The xmlDsoTicker() function is called if the ticker fails. xmlDsoTicker() has all the same arguments as initTicker() except the xmlFile argument, since the XML file has already been loaded. xmlDsoTicker() checks whether the variable counter (initialized to maxMsgs) is less than maxMsgs-1. If yes, the moveNext() method points to the next data item in tickerSet.

The BODY of the HTML page contains the following code:

				
 
Figure 1: Here is the output from the ticker app.

In this code, the tag has as its datafld the URL of the XML file. The tag has as its datafld the message of the XML file. The message is displayed in the element and the entire message is a hyperlink to the corresponding URL of that message.

Thus, the and elements contain the next data item (URL and message). The and then point to the next data after the specified delay. This happens as long as counterAnother Use for the XML-DSO
You can also use the XML-DSO to manipulate relatively small amounts of data (in the order of KBs) in situations that don’t require a database system.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.

©2023 Copyright DevX - All Rights Reserved. Registration or use of this site constitutes acceptance of our Terms of Service and Privacy Policy.

Sitemap