您好,欢迎访问一九零五行业门户网

xml应用的示例代码分享

这一节示范了一个小型的xml应用程序框架。
--------------------------------------------------------------------------------
从xml文档开始
首先我们建立一个简单的xml文档。
来看一下我们原始的xml文档,描述了cd目录。
<?xml version="1.0" encoding="iso-8859-1"?> <catalog> <cd> <title>empire burlesque</title> <artist>bob dylan</artist> <country>usa</country> <company>columbia</company> <price>10.90</price> <year>1985</year> </cd>
.
.
... more ...
.
--------------------------------------------------------------------------------
将xml文档载入数据岛
数据岛可以访问xml文件。
通过数据岛,可以把xml文档引入到html页面中。
<xml src="cd_catalog.xml" id="xmldso" async="false"> </xml>
使用上面示例代码,可以把cd_catalog.xml文件载入一个叫"xmldso"的数据岛中。属性async="false"的作用是在html处理器开始处理xml数据以前,必须确保xml文档中的所有数据都被载入到内存中。
--------------------------------------------------------------------------------
把xml数据绑定到html的表格元素中
html中的table元素可以用来显示xml数据。
为了使你的xml数据能在html页面中显示出来,必须把的数据岛绑定到一个html元素上。
把xml数据绑定到table元素,需要在table属性添加一个资源属性,并且在span元素中添加字段属性:
<table datasrc="#xmldso" width="100%" border="1"> <thead> <th>title</th> <th>artist</th> <th>year</th> </thead> <tr align="left"> <td><span datafld="title"></span></td> <td><span datafld="artist"></span></td> <td><span datafld="year"></span></td> </tr></table>
--------------------------------------------------------------------------------
把数据岛绑定到<span> 或者<p> 元素上
<span>或<p>元素都可以用来显示xml数据。
没有必要使用table元素来显示xml数据,数据可以通过数据岛绑定到任何一个html元素上。
所要做的就是在你得页面中添加一些<span> 或者<p>元素,使用数据资源属性把每一个元素和xml文档元素绑定起来,就象下面这样:
<br />title: <span datasrc="#xmldso" datafld="title"></span> <br />artist: <span datasrc="#xmldso" datafld="artist"></span> <br />year: <span datasrc="#xmldso" datafld="year"></span>
或者象这样的形式:
<br />title: <p datasrc="#xmldso" datafld="title"></p> <br />artist: <p datasrc="#xmldso" datafld="artist"></p> <br />year: <p datasrc="#xmldso" datafld="year"></p>
注意如果你使用<p>元素,数据将会显示在新的一行里。
上面的例子,可以看到xml数据都显示在一行里。如果要控制数据换行,就必须在你的代码里添加一些脚本程序。
--------------------------------------------------------------------------------
为你的xml数据添加导航脚本
可以通过脚本程序来实现导航功能。
添加数据岛方法,使用脚本函数movenext() 和 moveprevious() 来实现导航功能。
<script type="text/javascript"> function movenext() { x=xmldso.recordset if (x.absoluteposition < x.recordcount) { x.movenext() } } function moveprevious() { x=xmldso.recordset if (x.absoluteposition > 1) { x.moveprevious() } } </script>
--------------------------------------------------------------------------------
小结
如果你有创造力的话就可以编写出非常完善的应用程序。
如果你使用在这一页中学到的知识,在发挥一下想象力,就可以轻易的创造出完善的应用程序。
以上就是xml应用的示例代码分享的详细内容。
其它类似信息

推荐信息