11_XML DOM 遍历节点树
来源:互联网 发布:管家婆支持远程网络吗 编辑:程序博客网 时间:2024/06/05 10:05
XML DOM 遍历节点树
遍历(Traverse)意味着在节点树中进行循环或移动。
遍历节点树
通常您想要循环 XML 文档,比如:当您需要提取每个元素的值时。
这叫做"遍历节点树"。
下面的实例遍历 <book> 的所有子节点,并显示他们的名称和值:
实例
<html>
<head>
<script src="loadxmlstring.js"></script>
</head>
<body>
<script>
text="<book>";
text=text+"<title>Everyday Italian</title>";
text=text+"<author>Giada De Laurentiis</author>";
text=text+"<year>2005</year>";
text=text+"</book>";
xmlDoc=loadXMLString(text);
// documentElement always represents the root node
x=xmlDoc.documentElement.childNodes;
for (i=0;i<x.length;i++)
{
document.write(x[i].nodeName);
document.write(": ");
document.write(x[i].childNodes[0].nodeValue);
document.write("
");
}
</script>
</body>
</html>
<head>
<script src="loadxmlstring.js"></script>
</head>
<body>
<script>
text="<book>";
text=text+"<title>Everyday Italian</title>";
text=text+"<author>Giada De Laurentiis</author>";
text=text+"<year>2005</year>";
text=text+"</book>";
xmlDoc=loadXMLString(text);
// documentElement always represents the root node
x=xmlDoc.documentElement.childNodes;
for (i=0;i<x.length;i++)
{
document.write(x[i].nodeName);
document.write(": ");
document.write(x[i].childNodes[0].nodeValue);
document.write("
");
}
</script>
</body>
</html>
输出:
title: Everyday Italian
author: Giada De Laurentiis
year: 2005
author: Giada De Laurentiis
year: 2005
尝试一下 »
实例解释:
- loadXMLString() 把 XML 字符串载入 xmlDoc 中
- 获取根元素的子节点
- 输出每个子节点的节点名称以及文本节点的节点值
0 0
- 11_XML DOM 遍历节点树
- 04_XML DOM 节点树
- 03_XML DOM 节点
- 08_XML DOM - 访问节点
- 09_XML DOM 节点信息
- 10_XML DOM 节点列表
- 13_XML DOM - 导航节点
- 16_XML DOM 删除节点
- 17_XML DOM 替换节点
- 18_XML DOM 创建节点
- 19_XML DOM 添加节点
- 20_XML DOM 克隆节点
- 22_XML DOM 节点类型
- 14_XML DOM 获取节点值
- 15_XML DOM 改变节点值
- XML DOM遍历节点树
- jquery(遍历-dom节点)
- 15_XML DOM
- 10_XML DOM 节点列表
- python中类方法,静态方法和实例方法的区别
- [代码题]Simple_Virtual_World
- HLMT5Canvas标签五星制作
- python处理json格式的文件
- 11_XML DOM 遍历节点树
- 12_XML DOM 浏览器差异
- 1 介绍
- hibernate注解
- 13_XML DOM - 导航节点
- fedora9安装中文输入法并调试
- 回溯法与树的遍历
- POJ1028 模拟,水
- 浅淡ISP