The XML data source object is an ActiveX control that allows you to manipulate data between an XML file and an HTML page. This article will show you how to extract data from various XML data sources and how to display it using JavaScript.
XML data source object DSO is a Microsoft ActiveX control built on versions after Microsoft IE4. This object allows you to extract an external XML file or content embedded in an HTML file into an HTML page.
You can use XML - DSO to select content from an external XML file in a web page, extract XML data from the XML embedded in the web page, and then manipulate this data using JavaScript. However, it is not recommended to use this object in the Internet, as DSO can only work in browsers above MSIE 4, so this may bring some compatibility issues. Therefore, it is very appropriate to use XML-DSO on the intranet of the enterprise.
start
To initialize XML-DSO objects, we use the <OBJECT> 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 this control in a web page:
<OBJECT ID="SomeID" CLASSID="CLSID:550dda30-0541-11d2-9ca9-0060b0ec3d39"></OBJECT>
Although most objects require many parameters to be associated with, XML-DSO does not require any parameters.
Disconnect data using an XML data island
First, include an XML data island by using the <XML> tag. Secondly, assign it an ID, xmldb -- for future use. The data is actually extracted using HTML tags: <ALT>, <SPAN>, <DIV>, etc. The code in Code List 1 uses the <SPAN> tag. The datasrc attribute specifies the data island from which you want to extract data. The datafld property specifies the XML tag for the data you want. Therefore, the first <SPAN> extracts the name, and the second <SPAN> extracts the gender.
Code List 1:
<!-- -->
<html>
<head>
<title>XML </title>
</head>
<body bgcolor="#FFFFFF">
<xml >
<db>
<member>
<name>Premshree Pillai<name>
<sex>male</sex>
</member>
<member>
<name>Vinod</name>
<sex>male</sex>
</member>
</db>
</xml>
<span datasrc="#xmldb" datafld="name"<</span>
<br>
<span datasrc="#xmldb" datafld="sex"></span>
</body>
</html>
Note that this code does not initialize an XML-DSO object. This is because an XML data island has been created implicitly in use. 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 Code List 2 uses the <TABLE> tag to extract all instances:
The output will be:
Name Sex
Premshree Pillai male
Vinod male
In code list 2, the <TABLE> tag uses the <DIV> tag within the <TD> tag to extract data. The table will automatically repeat each instance of <member> (the parent tags of <name> and <sex>). Code List 2:
<!-- -->
<html>
<head>
<title>XML </title>
</head>
<body bgcolor="#FFFFFF">
<xml >
<db>
<member>
<name>Premshree Pillai<name>
<sex>male</sex>
</member>
<member>
<name>Vinod</name>
<sex>male</sex>
</member>
</db>
</xml>
<table datasrc="#xmldb" border="1">
<thead>
<th>Name</th>
<th>Sex</th>
</thead>
<tr>
<td><div datafld="name"></div></td>
<td><div datafld="sex"></div></td>
</tr>
</table>
</body>
</html>
Extract data using external XML files
In order to load an external XML file using XML-DSO, you must explicitly include this object and use some JavaScript.
First create an XML-DSO object, using ID myXML. Add width and height attributes to the <OBJECT> tag and set their value to 0. This ensures that XML-DSO objects do not occupy any space on your web page.
Secondly, use datasrc to create a table like myXML-simil-like in code list 2. The code uses the <DIV> tag (in the TD tag), uses datafld as the information in the first column, and uses the URL as the second column. Add the <SCRIPT> tag because here, external XML uses Java scripts to explicitly declare the XML file you want to load.
Set the variable xmlDso to. myXML references the object you have created. Next, use the load() method of XML-DSO to load. The file is connected to the object myXML.
<!-- -->
<?xml version="1.0" ?>
<ticker>
<item>
<message>JavaScript Ticker using XML DSO</message>
<URL></URL>
</item>
</ticker>
Now, look at the following HTML page:
<!-- -->
<html>
<head>
<title>XML </title>
<script language="JavaScript">
function load() {
var xmlDso=;
("");
}
</script>
</head>
<body bgcolor="#FFFFFF" onLoad="load()">
<object 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
The script above is very special. Here is a more general script:
<script language="JavaScript">
var xmlDso;
function load(xmlFile, objName) {
eval(''xmlDso=''+objName+''.XMLDocument'');
(xmlFile);
}
</script>
Now, to load any XML file use:
load("","anyXmlDsoObject");(csdn) (Source: PConline)