由于mozilla没有内建的机制支持xml数据岛,但是mozilla提供了对dom的支持,我们可以通过dom访问xml数据岛中节点、属性等。 下面就是一个比较全面的例子:
<?xml version="1.0" encoding="utf-8"?>
<!doctype html public "-//w3c//dtd xhtml 1.0 frameset//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style type="text/css">
xml
{
width: 0px;
height: 0px;
display: none;
}
</style>
<script language="javascript">
<!-- <![cdata[
function setevents() {
mozilladso();
}
function showdso() {
if(window.navigator.appname=="microsoft inte.net explorer")
{
alert(xmlone.xmldocument.xml)
alert(xmltwo.xmldocument.xml)
}
else
{
for(var i=0;i < document.getelementsbytagname('xml').length;i++)
alert(document.getelementsbytagname('xml').item(i).innerhtml);
}
}
function mozilladso() {
/* function: mozilladso
creation date: april 16, 2003
programmer: edmond woychowsky
purpose: the purpose of this function is to perform binding of
xml data islands for mozilla. the logic herein is bypassed
for microsoft inte.net explorer.
update date: programmer: description:
*/
// global variables
objxmldi = new collection(); // xml data island collection
objbound = new collection(); // bound xhtml object collection
// local variables
var objdatafld; // table datafld collection
var objrow; // table row
var rework = new regexp('inte.net explorer','gi');
var arrkeys = new array();
var intkey = 0; // objbound collection key
var introws; // row count
if(!rework.test(navigator.appname)) {
// locate data islands
for(var i=0;i < document.getelementsbytagname('xml').length;i++)
objxmldi.add('#' + document.getelementsbytagname('xml').item(i).getattribute('id'),document.getelementsbytagname('xml').item(i));
// locate bound nodes
for(var i=0;i < document.getelementsbytagname('table').length;i++)
if(document.getelementsbytagname('table').item(i).getattribute('datasrc') != null) {
objbound.add(intkey.tostring(),new boundxml());
objbound.item(intkey.tostring()).datasrc = document.getelementsbytagname('table').item(i).getattribute('datasrc');
objbound.item(intkey.tostring()).node = document.getelementsbytagname('table').item(i);
objbound.item(intkey.tostring()).nodename = document.getelementsbytagname('table').item(i).nodename;
objdatafld = new collection(); // reset collection;
introws = 0; // reset row count
// remove all rows
if(objbound.item(intkey.tostring()).node.getelementsbytagname('tr').length != 0) {
for(var j=0;j < objbound.item(intkey.tostring()).node.getelementsbytagname('tr').length;j++)
objrow = objbound.item(intkey.tostring()).node.lastchild.removechild(objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j));
for(var j=0;j < objbound.item(intkey.tostring()).node.getelementsbytagname('tr').length;j++)
objrow = objbound.item(intkey.tostring()).node.lastchild.removechild(objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j));
}
// determine bound nodes
for(var j=0;j < objrow.getelementsbytagname('input').length;j++)
if(!objdatafld.exists(objrow.getelementsbytagname('input').item(j).getattribute('datafld')))
objdatafld.item(objrow.getelementsbytagname('input').item(j).getattribute('datafld'),null);
for(var j=0;j < objrow.getelementsbytagname('div').length;j++)
if(!objdatafld.exists(objrow.getelementsbytagname('div').item(j).getattribute('datafld')))
objdatafld.item(objrow.getelementsbytagname('div').item(j).getattribute('datafld'),null);
for(var j=0;j < objrow.getelementsbytagname('span').length;j++)
if(!objdatafld.exists(objrow.getelementsbytagname('span').item(j).getattribute('datafld')))
objdatafld.item(objrow.getelementsbytagname('span').item(j).getattribute('datafld'),null);
arrkeys = objdatafld.keys();
// determine row count
for(var j=0;j < arrkeys.length;j++)
if(introws < objxmldi.item(objbound.item(intkey.tostring()).datasrc).getelementsbytagname(arrkeys[0]).length)
introws = objxmldi.item(objbound.item(intkey.tostring()).datasrc).getelementsbytagname(arrkeys[0]).length;
for(var j=0;j < introws;j++)
objbound.item(intkey.tostring()).node.lastchild.appendchild(objrow.clonenode(true));
// rebuild table and bind
for(var j=0;j < objbound.item(intkey.tostring()).node.getelementsbytagname('tr').length;j++)
for(var k=0;k < objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').length;k++)
for(var l=0;l < objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.length;l++)
switch(objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.item(l).nodename.tolowercase()) {
case('input'):
try {
if(objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.item(l).getattribute('type') != 'checkbox')
objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.item(l).value =
objxmldi.item(objbound.item(intkey.tostring()).datasrc).getelementsbytagname(objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.item(l).getattribute('datafld')).item(j).firstchild.nodevalue;
else
objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.item(l).checked = eval(objxmldi.item(objbound.item(intkey.tostring()).datasrc).getelementsbytagname(objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.item(l).getattribute('datafld')).item(j).firstchild.nodevalue);
}
catch(e) { }
if(objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.item(l).getattribute('type') != 'checkbox')
objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.item(l).onchange = new function('try { objxmldi.item(\'' + objbound.item(intkey.tostring()).datasrc + '\').getelementsbytagname(\'' + objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.item(l).getattribute('datafld') + '\').item(' + j.tostring() + ').firstchild.nodevalue = this.value } catch(e) { objxmldi.item(\'' + objbound.item(intkey.tostring()).datasrc + '\').getelementsbytagname(\'' + objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.item(l).getattribute('datafld') + '\').item(' + j.tostring() + ').appendchild(document.createtextnode(this.value)) };mozilladso;');
else
objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.item(l).onchange = new function('try { objxmldi.item(\'' + objbound.item(intkey.tostring()).datasrc + '\').getelementsbytagname(\'' + objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.item(l).getattribute('datafld') + '\').item(' + j.tostring() + ').firstchild.nodevalue = this.checked } catch(e) { objxmldi.item(\'' + objbound.item(intkey.tostring()).datasrc + '\').getelementsbytagname(\'' + objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.item(l).getattribute('datafld') + '\').item(' + j.tostring() + ').appendchild(document.createtextnode(this.checked)) };mozilladso;');
break;
case('div'):
case('span'):
try { // text node exists
objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.item(l).firstchild.nodevalue = objxmldi.item(objbound.item(intkey.tostring()).datasrc).getelementsbytagname(objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.item(l).getattribute('datafld')).item(j).firstchild.nodevalue;
}
catch(e) { // create text node
try {
objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.item(l).appendchild(document.createtextnode(objxmldi.item(objbound.item(intkey.tostring()).datasrc).getelementsbytagname(objbound.item(intkey.tostring()).node.getelementsbytagname('tr').item(j).getelementsbytagname('td').item(k).childnodes.item(l).getattribute('datafld')).item(j).firstchild.nodevalue));
}
catch(e) { }
}
break;
}
++intkey;
}
// non-tabular datasrc/datafld
for(var i=0;i < document.getelementsbytagname('input').length;i++)
if(document.getelementsbytagname('input').item(i).getattribute('datafld') != null) {
objbound.add(intkey.tostring(),new boundxml());
if(document.getelementsbytagname('input').item(i).getattribute('datasrc') != null)
objbound.item(intkey.tostring()).datasrc = document.getelementsbytagname('input').item(i).getattribute('datasrc');
objbound.item(intkey.tostring()).datafld = document.getelementsbytagname('input').item(i).getattribute('datafld');
objbound.item(intkey.tostring()).node = document.getelementsbytagname('input').item(i);
objbound.item(intkey.tostring()).nodename = document.getelementsbytagname('input').item(i).nodename;
++intkey;
}
for(var i=0;i < document.getelementsbytagname('div').length;i++)
if(document.getelementsbytagname('div').item(i).getattribute('datafld') != null) {
objbound.add(intkey.tostring(),new boundxml());
if(document.getelementsbytagname('div').item(i).getattribute('datasrc') != null)
objbound.item(intkey.tostring()).datasrc = document.getelementsbytagname('div').item(i).getattribute('datasrc');
objbound.item(intkey.tostring()).datafld = document.getelementsbytagname('div').item(i).getattribute('datafld');
objbound.item(intkey.tostring()).node = document.getelementsbytagname('div').item(i);
objbound.item(intkey.tostring()).nodename = document.getelementsbytagname('div').item(i).nodename;
++intkey;
}
for(var i=0;i < document.getelementsbytagname('span').length;i++)
if(document.getelementsbytagname('span').item(i).getattribute('datafld') != null) {
objbound.add(intkey.tostring(),new boundxml());
if(document.getelementsbytagname('span').item(i).getattribute('datasrc') != null)
objbound.item(intkey.tostring()).datasrc = document.getelementsbytagname('span').item(i).getattribute('datasrc');
objbound.item(intkey.tostring()).datafld = document.getelementsbytagname('span').item(i).getattribute('datafld');
objbound.item(intkey.tostring()).node = document.getelementsbytagname('span').item(i);
objbound.item(intkey.tostring()).nodename = document.getelementsbytagname('span').item(i).nodename;
++intkey;
}
arrkeys = objbound.keys();
// handle non-tabular binds
for(var i=0;i < arrkeys.length;i++)
switch(objbound.item(arrkeys[i]).nodename.tolowercase()) {
case('table'):
objbound.item(arrkeys[i]).rows = new array();
break;
case('input'):
try {
if(typeof(objbound.item(arrkeys[i]).datasrc) == 'string') {
try {
if(objbound.item(arrkeys[i]).node.getattribute('type') != 'checkbox')
objbound.item(arrkeys[i]).node.value = objxmldi.item(objbound.item(arrkeys[i]).datasrc).getelementsbytagname(objbound.item(arrkeys[i]).datafld).item(0).firstchild.nodevalue;
else
objbound.item(arrkeys[i]).node.checked = eval(objxmldi.item(objbound.item(arrkeys[i]).datasrc).getelementsbytagname(objbound.item(arrkeys[i]).datafld).item(0).firstchild.nodevalue);
}
catch(e) { }
if(objbound.item(arrkeys[i]).node.getattribute('type') != 'checkbox')
objbound.item(arrkeys[i]).node.onchange = new function('try { objxmldi.item(this.getattribute(\'datasrc\')).getelementsbytagname(this.getattribute(\'datafld\')).item(0).firstchild.nodevalue = this.value } catch(e) { objxmldi.item(this.getattribute(\'datasrc\')).getelementsbytagname(this.getattribute(\'datafld\')).item(0).appendchild(document.createtextnode(this.value)) };mozilladso()');
else
objbound.item(arrkeys[i]).node.onclick = new function('try { objxmldi.item(this.getattribute(\'datasrc\')).getelementsbytagname(this.getattribute(\'datafld\')).item(0).firstchild.nodevalue = this.checked } catch(e) { objxmldi.item(this.getattribute(\'datasrc\')).getelementsbytagname(this.getattribute(\'datafld\')).item(0).appendchild(document.createtextnode(this.checked)) };mozilladso()');
}
}
catch(e) { }
break;
case('div'):
case('span'):
if(typeof(objbound.item(arrkeys[i]).datasrc) == 'string') {
try { // text node exists
objbound.item(arrkeys[i]).node.firstchild.nodevalue = objxmldi.item(objbound.item(arrkeys[i]).datasrc).getelementsbytagname(objbound.item(arrkeys[i]).datafld).item(0).firstchild.nodevalue;
}
catch(e) { // create text node
try {
objbound.item(arrkeys[i]).node.appendchild(document.createtextnode(objxmldi.item(objbound.item(arrkeys[i]).datasrc).getelementsbytagname(objbound.item(arrkeys[i]).datafld).item(0).firstchild.nodevalue));
}
catch(e) { }
}
}
break;
default:
alert('error: unsupported html element - ' + objbound.item(arrkeys[i]).nodename.tolowercase());
break;
}
}
function boundxml() {
var datasrc = null; // data source (string)
var datafld = null; // data field (string)
var node = null; // xhtml node (object)
var nodename = null; // node name (string)
}
}
function collection() {
/* function: collection
creation date: august 16, 2002
programmer: edmond woychowsky
purpose: the purpose of this class is to define the collection
object associative array.
update date: programmer: description:
*/
// properties
this.objcollection = new object; // associative array
this.count = 0; // total numbers of items
// methods
this.add = coladd; // add method
this.exists = colexists; // exists method
this.item = colitem; // item method
this.removeall = colremoveall; // remove all method
this.remove = colremove; // remove method
this.keys = colkeys; // keys method
function coladd(strkey,stritem) {
/* function: coladd
creation date: august 16, 2002
programmer: edmond woychowsky
purpose: the purpose of this function is to add an item to
the collection object.
update date: programmer: description:
*/
if(typeof(this.objcollection[strkey]) == 'undefined')
++this.count;
this.objcollection[strkey] = stritem;
}
function colexists(strkey) {
/* function: colexists
creation date: august 16, 2002
programmer: edmond woychowsky
purpose: the purpose of this function is to return a boolean
indicating where or not a key exists in the
collection object.
update date: programmer: description:
*/
if(typeof(this.objcollection[strkey]) == 'undefined')
return false;
else
return true;
}
function colitem(strkey,stritem) {
/* function: colitem
creation date: august 16, 2002
programmer: edmond woychowsky
purpose: the purpose of this function is to either set or get
an item to or from the collection object.
update date: programmer: description:
*/
if(typeof(stritem) == 'undefined')
return this.objcollection[strkey];
else {
if(typeof(this.objcollection[strkey]) == 'undefined')
++this.count;
this.objcollection[strkey] = stritem;
}
}
function colremoveall() {
/* function: colremoveall
creation date: august 16, 2002
programmer: edmond woychowsky
purpose: the purpose of this function is to remove all keys
and items from the collection object and set the
item count to zero.
update date: programmer: description:
*/
this.objcollection = new object;
this.count = 0;
}
function colremove(strkey) {
/* function: colremove
creation date: august 16, 2002
programmer: edmond woychowsky
purpose: the purpose of this function is to remove a single
item and it's associated key from the collection
object and decrement the count by one.
update date: programmer: description:
*/
if(typeof(stritem) != 'undefined') {
this.objcollection[strkey] == 'undefined';
--this.count;
}
}
function colkeys() {
/* function: colkeys
creation date: august 16, 2002
programmer: edmond woychowsky
purpose: the purpose of this function is to return an array
consisting of the collection object's keys.
update date: programmer: description:
*/
var arrwork = new array();
var strkey;
for(strkey in this.objcollection)
if(this.objcollection[strkey] != 'undefined')
arrwork.push(strkey);
return arrwork;
}
}
// ]]> -->
</script>
</head>
<body onload="setevents()">
<table width="20%">
<tr>
<td>
<input type="text" id="input" datasrc="#xmlone" datafld="first" />
</td>
</tr>
<tr>
<td>
<div id="div" datasrc="#xmlone" datafld="second"></div>
</td>
</tr>
<tr>
<td>
<span id="span" datasrc="#xmlone" datafld="third"></span>
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="input" datasrc="#xmlone" datafld="check" />
</td>
</tr>
</table>
<table datasrc="#xmltwo" border="1">
<thead>
<td bgcolor="#ffffee">line</td>
<td bgcolor="#ffffee">word</td>
<td bgcolor="#ffffee">color</td>
<td bgcolor="#ffffee">true/false</td>
</thead>
<tr>
<td>
<div datafld="line"></div>
</td>
<td>
<span datafld="word"></span>
</td>
<td>
<input type="text" id="input" datafld="color" />
</td>
<td>
<input type="checkbox" id="input" datafld="check" />
</td>
</tr>
</table>
<br />
<input type="button" onclick="showdso()" value="show data islands" />
<xml id="xmlone" async="false">
<root>
<first></first>
<second>plugh</second>
<third></third>
<check></check>
</root>
</xml>
<xml id="xmltwo" async="false">
<root>
<row>
<line>1</line>
<word>one</word>
<color>red</color>
<check></check>
</row>
<row>
<line>2</line>
<word>two</word>
<color></color>
<check>false</check>
</row>
<row>
<line>3</line>
<word>three</word>
<color>blue</color>
<check>true</check>
</row>
</root>
</xml>
</body>
</html>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器