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 how to extract data from a variety of XML data sources and how to display that data using JavaScript.
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
in order to initialize the Xml-dso object, we use the <OBJECT> tag. The ClassID for XML-DSO is:
clsid:550dda30-0541-11d2-9ca9-0060b0ec3d39
This ID uniquely identifies the 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.
uses an XML data island to extract data
First, you include an XML data island by using the <XML> tag. Second, assign it a id,xmldb--for later use. The data is actually extracted using HTML tags: <ALT>, <SPAN>, <DIV>, and so on. The code in Code Listing 1 uses the <SPAN> 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 <SPAN> extracts the name, and the second <SPAN> extracts the gender.
Code Listing 1:
!--example1.htm-->
<title> XML dso-example1.htm </title>
<body bgcolor= "#FFFFFF" >
<xml id= "Xmldb" >
<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>
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. Code Listing 2 uses the <TABLE> tag to extract all instances:
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-->
<title> XML dso-example2.htm </title>
<body bgcolor= "#FFFFFF" >
<xml id= "Xmldb" >
<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>
uses an external XML file to extract data
to load an external XML file with XML-DSO, you must explicitly include the object and use some JavaScript.
first creates a Xml-dso object, using ID myxml. Add width and Height properties to the <OBJECT> 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 <DIV> tag (in TD-marked), uses DATAFLD as the first column of information, and uses the URL as the second column. Add the <SCRIPT> tag, because in this case, the external XML uses a Java script to explicitly declare the XML file you want to load.
Set Variable XMLDSO to myxml.xmldocument. myXML references the object you have created. Next, load the example3.xml using the XML-DSO load () method. The file Example3.xml is connected to the object myXML.
!--example3.xml-->
<?xml version= "1.0"? >
<ticker>
<item>
<message> JavaScript Ticker using XML DSO </message>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.