当前位置: 首页 > 网络学院 > XML相关教程 > XML DOM > DOM 遍历节点树

XML DOM
DOM Attribute
DOM Text
DOM CDATA
DOM Comment
DOM HttpRequest
DOM ParseError
DOM 校验器
DOM 介绍
DOM 摘要
DOM 案例
DOM 节点树
DOM 访问节点树
DOM 节点信息
DOM 文档执行
DOM 节点导航

XML DOM 中的 DOM 遍历节点树


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1307 ::
收藏到网摘: n/a

Examples
案例

Traverse node tree
遍历节点树
This example shows how to loop through all child nodes of <note>, and print the node name and the node value.
上述案例说明了对所有“<note>”子节点的循环操作方法,并打印了指定的节点名称和节点值。


Traversing the Node-tree
遍历节点树(Node-tree)

Often you will need to loop through elements in an XML document or string.
你可能经常需要对XML文档或字符串中的元素执行循环操作。

The example below loops through all child nodes of <note>, and prints the node name and the node value of each node:
下述案例说明了“<note>”子节点的循环方法,并打印了指定的节点名称和节点值:

<html>
<body>
<script type="text/javascript">
var text="<note>";
text=text+"<to>Tove</to>";
text=text+"<from>Jani</from>";
text=text+"<heading>Reminder</heading>";
text=text+"<body>Don't forget me this weekend!</body>";
text=text+"</note>";
// code for IE
if (window.ActiveXObject)
{
var doc=new ActiveXObject("Microsoft.XMLDOM");
doc.async="false";
doc.loadXML(text);
}
// code for Mozilla, Firefox, Opera, etc.
else
{
var parser=new DOMParser();
var doc=parser.parseFromString(text,"text/xml");
}
// documentElement always represents the root node
var x=doc.documentElement;
for (i=0;i<x.childNodes.length;i++)
{
document.write(x.childNodes[i].nodeName);
document.write("=");
document.write(x.childNodes[i].childNodes[0].nodeValue);
document.write("<br />");
}
</script>
</body>
</html>

Output:
输出结果:

to=Tove
from=Jani
heading=Reminder
body=Don't forget me this weekend!

评论 (0) All

登陆 | 还没注册?