Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Making Use of the XML Data Source Object  : Page 3

The XML Data Source Object is an ActiveX control that allows you to manipulate data between XML files and HTML pages. This article will show you the basics of how to extract content from various XML data sources and how to present that data using a little JavaScript.


advertisement
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 <OBJECT> 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 <DIV> tags (within TD tags) using datafld as the message for the first column and the URL for the second column. The <SCRIPT> tag is added because here the external XML uses JavaScript to explicitly state the XML file you want to load.



Set the variable xmlDso to myXML.XMLDocument. myXML refers to the object you have created. Next, load example3.xml using the XML-DSO's load() method. The file example3.xml binds to the object, myXML.

<!-- 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 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:

<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");



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date