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:
<a href="" datasrc="#ticker" datafld="URL" class="tickerStyle">
<span datasrc="#ticker" datafld="message"></span>
|Figure 1: Here is the output from the ticker app.
In this code, the <A> tag has as its datafld the URL of the XML file. The <SPAN> tag has as its datafld the message of the XML file. The message is displayed in the <SPAN> element and the entire message is a hyperlink to the corresponding URL of that message.
Thus, the <A> and <SPAN> elements contain the next data item (URL and message). The <A> and <SPAN> then point to the next data after the specified delay. This happens as long as counter<maxMsgs-1 (counter is incremented each time). If counter<maxMsgs-1 is false, the counter is reset to 0 and points to the first data item in the tickerSet.
Another 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.