网络学院 w3pop社区 网络资源 IT新闻

w3pop.com :: 网络学院 :: XML DOM :: DOM 遍历节点树

会员登陆

帐号

密码

回答

记住密码

忘记密码? 注册

XML DOM
DOM 节点
DOM 节点列表
DOM 解析
DOM 遍历节点树
DOM Mozilla 和 I..
DOM 获取节点
DOM 设置节点
DOM 删除节点
DOM 更换节点
DOM 建立节点
DOM 添加节点
DOM 克隆节点
DOM 节点类型
DOM Node
DOM NodeList
DOM NamedNodeMap
DOM Document
DOM DocumentType
DOM ProcessingIn..
DOM Element

DOM 遍历节点树


作者:w3pop.com 翻译/整理:w3pop.com 发布:2007-04-29 修改:2007-08-30 浏览:5048 :: ::

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