欢迎光临
我们一直在努力

xml dom介绍和例子(三)_asp教程

建站超值云服务器,限时71元/月

10.为XML文件提供HTML格式
    XML的一个优点是把HTML文档和它的数据分离开。通过使用浏览器中的XML parser,HTML页面可以被构造成静态文档,
通过JavaScript提供动态数据。下面的例子使用JavaScript读取XML文档,写XML数据成HTML元素:
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
    
nodes = xmlDoc.documentElement.childNodes
    
to.innerText = nodes.item(0).text
from    .innerText = nodes.item(1).text
header.innerText = nodes.item(2).text
body.innerText = nodes.item(3).text

11.通过名称访问XML元素
    下面的例子使用JavaScript读取XML文档,写XML数据成HTML元素:
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
    
document.write(xmlDoc.getElementsByTagName("from").item(0).text)

12.装载纯XML文本进入parser
    下面的代码装载文本字符串进入XML parser :
<script language="JavaScript">
var text="<note>"
text=text+"<to>Tove</to><from>Jani</from>"
text=text+"<heading>Reminder</heading>"
text=text+"<body>Dont forget me this weekend!</body>"
text=text+"</note>"
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.loadXML(text)
// ……. processing the document goes here
</script>

13.装载XML进入Parser
<html>
<body>

<script language="javascript">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
    
document.write("The first XML element in the file contains: ")
    
document.write(xmlDoc.documentElement.childNodes.item(0).text)
</script>

</body>
</html>

遍历XML节点树:
<html>
<body>
<script language="VBScript">
txt="<h1>Traversing the node tree</h1>"
document.write(txt)
set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
for each x in xmlDoc.documentElement.childNodes
  document.write("<b>" & x.nodename & "</b>")
  document.write(": ")
  document.write(x.text)
  document.write("<br>")
next
</script>
</body>
</html>

装载XML 进入 HTML

<html>
<head>

<script language="JavaScript"
for="window" event="onload">

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
nodes = xmlDoc.documentElement.childNodes
to.innerText = nodes.item(0).text
from.innerText = nodes.item(1).text
header.innerText = nodes.item(2).text
body.innerText = nodes.item(3).text

</script>

<title>HTML using XML data</title>
</head>

<body bgcolor="yellow">
<h1>Refsnes Data Internal Note</h1>

<b>To: </b><span id="to"></span>

<br>
<b>From: </b><span id="from"></span>

<hr>
<b><span id="header"></span></b>

<hr>
<span id="body"></span>

</body>
</html>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » xml dom介绍和例子(三)_asp教程
分享到: 更多 (0)