How to use XML data source objects in Web design

Source: Internet
Author: User
The xml data source object is an ActiveX control that allows you to operate 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 use javaScript to display the data. The xml data source object is an ActiveX control that allows you to operate 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 use javaScript to display the data.

The XML data source object DSO is a Microsoft ActiveX control built on versions later than Microsoft IE4. This object allows you to extract an external XML file or content embedded in an HTML file to an HTML page.

You can use XML-DSO to select content from an external XML file on a Web page, extract XML data from the XML embedded in the Web page, and then use Javascript to operate on the data. However, it is not recommended to use this object on the Internet, because DSO can only work in browsers above MSIE 4, which may cause compatibility issues. Therefore, it is very appropriate to use XML-DSO in the enterprise Intranet.

Start

To initialize an XML-dso object, 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 the control on a Web page:

  <OBJECT ID="SomeID" CLASSID="CLSID:550dda30-0541-11d2-9ca9-0060b0ec3d39"></OBJECT>

While most objects require many parameters to be associated with, the XML-DSO does not require any parameters.

Extract data using an XML data Island

First, you can use the <XML> tag to include an XML data Island. Next, assign it an ID, xmldb -- for future use. Data is actually extracted using HTML tags: <ALT>, <SPAN>, <DIV>, and so on. The code in Code List 1 uses the <SPAN> flag. The datasrc attribute specifies the data island from which you want to extract data. The Dataworks attribute specifies the XML tag of the data you want. Therefore, the first <SPAN> extracts the name, and the second <SPAN> extracts the gender.

Code List 1:

<!-- example1.htm --><html><head><title>XML DSO-example1.htm</title></head><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></html>

Note that this code does not initialize a XML-DSO object. This is because an XML data Island has been implicitly created. The output should be:
  
  

Premshree Pillai  male

Note that there are two <name> and <sex> tags in the XML data Island. With this method, you can only extract the first instance in these tags. The code in Code List 2 uses the <TABLE> flag 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 in the <TD> tag to extract data. The table will automatically repeat each instance of <member> (<name> and <sex>.

Code List 2:

<!-- example2.htm --><html><head><title>XML DSO-example2.htm</title></head><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><p datafld="name"></p></td><td><p datafld="sex"></p></td></tr></table></body></html>

Extract data using external XML files

To load an external XML file using a XML-DSO, you must explicitly include this object and use some JavaScript.

First create a XML-DSO object using ID myXML. Add the width and height attributes to the <OBJECT> tag and set them to 0. This ensures that XML-DSO objects do not occupy any space on your web page.

Second, use datasrc to create a table like myXML-similar to that in Code List 2. The code uses the <DIV> flag (marked in TD) to extract data, use Dataworks as the information in the first column, and use URL as the second column. Add the <SCRIPT> tag, because the external XML uses the Java SCRIPT to explicitly declare the XML file you want to load.

Set xmlDso to myXML. XMLDocument. MyXML references the created object. Next, load example3.xml using the load () method of the XML-DSO. File example3.xml is connected 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, study 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><p datafld="message"></p></td><td><p datafld="URL"></p></td></tr></table></body></html>

The output should be:

Message URL   JavaScript Ticker using XML DSO http://someURL.com

The above script is very special. A more general script is provided below:

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

Using XML-DSO and JavaScript

Suppose you have an XML file containing the name, email address, and phone number. You want to use it to build an application that displays each person's file-one at a time. The user will use the "Next" and "Previous" buttons to browse everyone's data. Javascript can help you achieve this purpose.

The following code uses the record set method to save all the data in the file to a variable memberSet. The moveNext () method points to the next data item (next row ). The script then loads the XML file example4.xml and saves the record to the variable memberSet. The first record is displayed, but memberSet. moveNext () points to the next record in the file relative to the previous specified data.

<!-- example4.xml --><?xml version="1.0" ?><myDB><member><name>Premshree Pillai</name><sex>male</sex></member><member><name>Vinod</name><sex>male</sex></member><member><name>Santhosh</name><sex>male</sex></member></myDB>

Here is the corresponding HTML file:

<!-- example4.htm --><html><head><title>XML DSO-example4.htm</title><script language="JavaScript">function load() {var xmlDso=myDB.XMLDocument;xmlDso.load("example4.xml");/* Get the complete record set */var memberSet=myDB.recordset;/* Go to next data */memberSet.moveNext();}</script></head><body bgcolor="#FFFFFF" onLoad="load()"><object id="myDB" CLASSID="clsid:550dda30-0541-11d2-9ca9-0060b0ec3d39" width="0" height="0"></object><span datasrc="#myDB" datafld="name"></span></body></html>

The output should be:

Vinod

The following provides more methods to operate a XML-DSO using JavaScript:

· MovePrevious (): refers to the first data item.

· MoveFirst (): point to the first data item.

· MoveLast (): point to the last data item.

· EOF: This attribute is used to check whether we have reached the bottom of the data record.

Using XML-DSO and JavaScript

Suppose you have an XML file containing the name, email address, and phone number. You want to use it to build an application that displays each person's file-one at a time. The user will use the "Next" and "Previous" buttons to browse everyone's data. Javascript can help you achieve this purpose.

The following code uses the record set method to save all the data in the file to a variable memberSet. The moveNext () method points to the next data item (next row ). The script then loads the XML file example4.xml and saves the record to the variable memberSet. The first record is displayed, but memberSet. moveNext () points to the next record in the file relative to the previous specified data.

<!-- example4.xml --><?xml version="1.0" ?><myDB><member><name>Premshree Pillai</name><sex>male</sex></member><member><name>Vinod</name><sex>male</sex></member><member><name>Santhosh</name><sex>male</sex></member></myDB>

Here is the corresponding HTML file:

<!-- example4.htm --><html><head><title>XML DSO-example4.htm</title><script language="JavaScript">function load() {var xmlDso=myDB.XMLDocument;xmlDso.load("example4.xml");/* Get the complete record set */var memberSet=myDB.recordset;/* Go to next data */memberSet.moveNext();}</script></head><body bgcolor="#FFFFFF" onLoad="load()"><object id="myDB" CLASSID="clsid:550dda30-0541-11d2-9ca9-0060b0ec3d39" width="0" height="0"></object><span datasrc="#myDB" datafld="name"></span></body></html>

The output should be:

   Vinod

The following provides more methods to operate a XML-DSO using JavaScript:

· MovePrevious (): refers to the first data item.

· MoveFirst (): point to the first data item.

· MoveLast (): point to the last data item.

· EOF: This attribute is used to check whether we have reached the bottom of the data record.

InitTicker () first checks whether IE 4 + exists. If the browser is IE4 +, the XML file is passed and loaded as a parameter. If the timer fails, call the xmlDsoTicker () function. In addition to the xmlFile parameter, xmlDsoTicker () has the same parameters as initTicker () because the XML file has been loaded. XmlDsoTicker () checks whether the variable counter (initially maxMsgs) is smaller than the maxMsgs-1. If yes, the moveNext () method points to the next data item in the tickerSet.

The BODY of the HTML page contains the following code:

<a href="" datasrc="#ticker" datafld="URL" class="tickerStyle"><span datasrc="#ticker" datafld="message"></span></a>

: Display output of the timer application.

In this code, <A> mark the URL of the XML file as its Dataworks. <SPAN> mark the information of the XML file as its Dataworks. This information is displayed in the <SPAN> element, and the entire information can be connected through the URL corresponding to this 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 happens as long as counter <maxMsgs-1 (counter increments each time. If counter <the maxMsgs-1 is false, the counter is set to 0 and then points to the first data item in the tickerSet.

The above is a detailed description of how to use XML data source objects in Web design. For more information, see PHP Chinese network (www.php1.cn )!

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.