★☆

★☆
posts - 0, comments - 0, trackbacks - 0, articles - 80
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

替换节点

Posted on 2008-12-05 11:38 阅读(202) 评论(0)  编辑  收藏 所属分类: javascript操作XML


<%@ page contentType="text/html; charset=GBK"%>
<html>
<head>
<script type="text/javascript" src="loadxmldoc.js">
</script>
</head>
<body>
先得到父节点,然后构造一个新节点,将新节点替换成旧节点。
<script type="text/javascript">

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.documentElement;

//创建一个 book 元素、一个 title 元素,以及一个 text 节点
newNode=xmlDoc.createElement("book");
newTitle=xmlDoc.createElement("title");
newText=xmlDoc.createTextNode("Hello World");

//向 title 节点添加文本节点
newTitle.appendChild(newText);

//向 book 节点添加 title 节点
newNode.appendChild(newTitle);

y=xmlDoc.getElementsByTagName("book")[0];

//用这个新节点替换第一个 book 节点
x.replaceChild(newNode,y);

</script>
</body>
</html>