Read and Display Server-Side XML with JavaScript Article

Share this article

Project: An XML-based JavaScript Ticker

There are many more properties and methods available, and, using these, you can create many client side applications. The main advantage of using XML with JavaScript is that editing data becomes very easy. As XML is structured, it makes the management of content very easy. One example is a folder-tree menu. Another one is a JavaScript Ticker. You can find the full code an an example of this XML-based JavaScript Ticker at DynamicDrive.

We will create a XML based JavaScript Ticker that can display any number of messages. The ticker reads its contents (i.e. the ticker style), the text to be displayed, and the link for that particular message from an XML file. We’ll call the XML file ticker_items.xml.

The structure of the XML document is as follows:

<?xml version="1.0"?>  
   pause       = "true" / "false"       "true" for pause onMouseOver  
   timeout     = positive integer       The delay in seconds b/w messages  
   border      = positive integer       The border width of Ticker  
   bordercolor = #HexColor              The border color of Ticker  
   background  = #HexColor              The background color of Ticker  
   width       = positive integer       Ticker width  
   height      = positive integer       Ticker height  
     font       = "verdana,arial,helvetica..."     Ticker link font  
     color      = #HexColor                        Ticker link color  
     decoration = "none" / "underline" /  
                  "underline + overline"           Ticker link style  
     weight     = "normal" / "bold"                Ticker link weight  
     size       = <positive integer>pt             Ticker link size  
     font       = "verdana,arial,hevetica..."      Ticker link font  
     color      = #HexColor                        Ticker link color  
     decoration = "none" / "underline" /  
                  "underline + overline"           Ticker link style  
     weight     = "normal" / "bold"                Ticker link weight  
     size       = <positive integer>pt             Ticker link size  
   URL       = A valid URL                         Ticker link URL  
   target    = "_blank" / "_top" / "_self" /  
               <any other valid target name>       Ticker link target  
 > Ticker item 1 text </tickeritem>  
 <tickeritem ...> Ticker item 2 text </tickeritem>  
XML Ticker Script
<script language="JavaScript1.2">  
// XML Ticker JavaScript  
// (c) 2002 Premshree Pillai  
// Use freely as long as all messages are as it is  
// Location of script:  
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");  
function loadXML(xmlFile)  
function verify()  
if (xmlDoc.readyState != 4)  
 return false;  
document.write('<style type="text/css">');  
document.write('.ticker_style{font-family:' +  
ticker.childNodes(1).childNodes(0).getAttribute('font') + ';  
font-size:' +  
+ '; color:' +  
ticker.childNodes(1).childNodes(0).getAttribute('color') +  
'; font-weight:' +  
ticker.childNodes(1).childNodes(0).getAttribute('weight') +  
'; text-decoration:' +  
+ '}');document.write('.ticker_style:hover{font-family:' +  
ticker.childNodes(1).childNodes(1).getAttribute('font') +  
'; font-size:' +  
+ '; color:' +  
ticker.childNodes(1).childNodes(1).getAttribute('color') +  
'; font-weight:' +  
ticker.childNodes(1).childNodes(1).getAttribute('weight') +  
'; text-decoration:' +  
('decoration') + '}<br>');  
document.write('<table style="border:' +  
+ ' solid ' + ticker.childNodes(0).getAttribute('bordercolor') +  
'; background:' + ticker.childNodes(0).getAttribute('background') +  
'; width:' + ticker.childNodes(0).getAttribute('width') + '; height:'  
+ ticker.childNodes(0).getAttribute('height') + '">  
<tr><td><div id="ticker_space"></div>  
var item_count=2;  
var timeOutVal=(ticker.childNodes(0).getAttribute('timeout'))*1000;  
var original_timeOutVal=timeOutVal;  
var isPauseContent;  
isPauseContent=' onmouseover="setDelay();" onmouseout="reset();"';  
function setTicker()  
document.all.ticker_space.innerHTML='<center><a href="' +  
ticker.childNodes(item_count).getAttribute('URL') + '" target="'  
+ ticker.childNodes(item_count).getAttribute('target') +  
'" class="ticker_style"' + isPauseContent + '>' +    
ticker.childNodes(item_count).firstChild.text + '</a></center>';  
function setDelay()  
function reset()  

As you can see in the source code, the ticker reads:

  • all the messages to be displayed,
  • the links for each message,
  • the target for each URL,
  • the ticker static style,
  • the roll-over style,
  • border width, color and background,
  • the delay between messages, and more, from the XML file.
  • So if you want to change any parameter of the Ticker, all you have to do is make necessary changes in the XML file.

    The ticker shown here is a basic ticker that rotates messages at an interval that is specified in the XML file. There are many effects you could add to the ticker like ‘Fading message’ or ‘Teletypewriter’. You could also add features to change the ticker speed, or to list all messages in an instant!

    Go to page: 1 | 2
    Premshree PillaiPremshree Pillai
    View Author

    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.

    Share this article
    Read Next
    Get the freshest news and resources for developers, designers and digital creators in your inbox each week
    Loading form