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 how to extract data from a variety of XML data sources and how to display that data using JavaScript. The
XML Data source object DSO is a Microsoft ActiveX control built on a later version of Microsoft IE4. This object allows you to extract content from an external XML file or embedded HTML file into an HTML page.
You can use XML-DSO in a Web page to pick content from an external XML file, extract the XML data from the XML that is embedded in the Web page, and then manipulate the data using JavaScript. However, it is not recommended to use this object on the Internet because DSO can only work in browsers above MSIE 4, so this can lead to some compatibility issues. Therefore, it is appropriate to use XML-DSO in intranet.
Start
to initialize the Xml-dso object, we use the tag. The ClassID for XML-DSO is:
clsid:550dda30-0541-11d2-9ca9-0060b0ec3d39
This ID uniquely identifies XML-DSO. Use the following code to initialize the control in a Web page:
Although most objects require many parameters to be associated with them, XML-DSO does not require any parameters.
Use an XML data island to extract data
First, include an XML data island by using the tag. Second, assign it a id,xmldb--for later use. The data is actually extracted using HTML tags: , ,
, and so on. The code in Code Listing 1 uses the tag. The Datasrc property specifies the data island from which you want to extract the data. The DATAFLD property specifies the XML tag of the data you want. So, the first extracts the name, and the second extracts the gender.
Code Listing 1:
!--example1.htm-->
XML dso-example1.htm
Premshree Pillai male Vinod male
|
Note that this code does not initialize a Xml-dso object. This is because one has been implicitly created in the use of the XML data island. The output should be:
Premshree Pillai
Male
Note that there are two <name> and <sex> tags in the XML data island. Using this method, you can only extract the first instance of these tags. The code in Listing 2 uses the <TABLE> tag to extract all instances:
The output will be:
Name |
Sex |
Premshree Pillai |
Male |
Vinod |
Male |
In code Listing 2, the <TABLE> tag extracts data using the <DIV> tag in the <TD> tag. The table automatically repeats each instance of the <member> (<name> and <sex> parent tags).
Code Listing 2:
!--example2.htm-->
XML dso-example2.htm
Premshree Pillai male Vinod male
|
Extracting data using an external XML file
To load an external XML file with XML-DSO, you must explicitly include the object and use some JavaScript.
First create a Xml-dso object, using the ID myxml. Add width and Height properties to the tag, and then set their value to 0. This ensures that the Xml-dso object does not occupy any space on your Web page.
Second, use DATASRC to create a table like myXML--similar to code Listing 2. The code extracts data using the
tag (in TD-marked), uses DATAFLD as the first column of information, and uses the URL as the second column. Add the
!--Example3.xml--> > JavaScript Ticker using XML DSO http://someURL.com |
Now, look at the following HTML page:
!--example3.htm-->
XML dso-example3.htm
Width= "0" height= "0" >
|
The output should be:
Message URL
JavaScript Ticker using XML DSO http://someURL.com
The script above is very specialized. A more general script is given below:
Now, to load any XML file with: Load ("Somexmlfile.xml", "Anyxmldsoobject"); |
using XML-DSO and JavaScript
Suppose you have an XML file that contains your name, e-mail address, and phone number. You want to use it to build an application that shows everyone's file--one at a time. Users will use the "Next" and "Previous" buttons to browse each person's data. JavaScript can help you achieve this.
The following code uses the Recordset method to save all the data in a file to a variable memberset. The MoveNext () method points to the next data item (the next line). The script then loads the XML file Example4.xml and saves the record to the variable memberset. The first record will be displayed, but Memberset.movenext () points to the next record in the file relative to the previous specified data.
!--Example4.xml--> > Premshree Pillai male Vinod male Santhosh male |
Here is the corresponding HTML file:
!--example4.htm-->
XML dso-example4.htm
Width= "0" height= "0" >
|
The output should be:
Vinod
Here are some more ways to use JavaScript to manipulate XML-DSO:
· MovePrevious (): points to the previous data item.
· MoveFirst (): points to the first data item.
· MoveLast (): Point to last data item.
· EOF: This property is used to detect whether we have reached the bottom of the data record.
using XML-DSO and JavaScript
Suppose you have an XML file that contains your name, e-mail address, and phone number. You want to use it to build an application that shows everyone's file--one at a time. Users will use the "Next" and "Previous" buttons to browse each person's data. JavaScript can help you achieve this.
The following code uses the Recordset method to save all the data in a file to a variable memberset. The MoveNext () method points to the next data item (the next line). The script then loads the XML file Example4.xml and saves the record to the variable memberset. The first record will be displayed, but Memberset.movenext () points to the next record in the file relative to the previous specified data.
!--Example4.xml--> > Premshree Pillai male Vinod male Santhosh male |
Here is the corresponding HTML file:
!--example4.htm-->
XML dso-example4.htm
Width= "0" height= "0" >
|
The output should be:
Vinod
Here are some more ways to use JavaScript to manipulate XML-DSO:
· MovePrevious (): points to the previous data item.
· MoveFirst (): points to the first data item.
· MoveLast (): Point to last data item.
· EOF: This property is used to detect whether we have reached the bottom of the data record.
Initticker () First check if there is IE 4+. If the browser is ie4+, the XML file is passed as a parameter and loaded. If the timer fails, then call the Xmldsoticker () function. Xmldsoticker () has the same parameters as Initticker () in addition to the xmlfile parameters, because the XML file is already loaded. Xmldsoticker () checks whether the variable counter (the initial value is MAXMSGS) is less than maxMsgs-1. If so, the MoveNext () method points to the next data item in Tickerset.
The body of the HTML page contains the following code:
Figure 1: The timer application displays the output.
In this piece of code, the <A> tag takes the URL of the XML file as its datafld. The <SPAN> tag takes the information of an XML file as its datafld. This information is displayed in the <SPAN> element, and the entire information can be connected through the corresponding URL of the information.
In this way, the <A> and <SPAN> elements contain the next data item (URL and information). After a delay, <A> and <SPAN> point to the next data. This action occurs as long as the counter<maxmsgs-1 (the counter increments each time). If Counter<maxmsgs-1 is false, the counter is set to 0 and then points to the first data item in Tickerset.