XML DOM节点操作:替换节点

replaceChild() 方法替换指定节点。

nodeValue 属性替换文本节点中的文本。

实例

下面的例子使用 XML 文件 books.xml

函数 loadXMLDoc(),位于外部 JavaScript 中,用于加载 XML 文件。替换元素节点本例使用 replaceChild() 来替换第一个 <book> 节点。替换文本节点中的数据本例使用 nodeValue 属性来替换文本节点中的数据。

替换元素节点

replaceChild() 方法用于替换节点。

下面的代码片段替换第一个 <book> 元素:

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);

例子解释:

  • 通过使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中
  • 创建一个新的元素节点 <book>
  • 创建一个新的元素节点 <title>
  • 创建一个新的文本节点,带有文本 “Hello World”
  • 向新元素节点 <title> 追加这个新文本节点
  • 向新元素节点 <book> 追加这个新元素节点 <title>
  • 用新的 <book> 元素节点替换第一个 <book> 元素节点
<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"> 
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/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);

z=xmlDoc.getElementsByTagName("title");
for (i=0;i<z.length;i++)
{
document.write(z[i].childNodes[0].nodeValue);
document.write("<br />");
}
</script>
</body>
</html>

替换文本节点中的数据

replaceData() 方法用于替换文本节点中的数据。

replaceData() 方法有三个参数:

  • offset – 在何处开始替换字符。Offset 值以 0 开始。
  • length – 要替换多少字符
  • string – 要插入的字符串
xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];

x.replaceData(0,8,"hello");

例子解释:

  • 通过使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中
  • 获取第一个 <title> 元素节点的文本节点
  • 使用 replaceDat 方法把文本节点的前 8 个字符替换为 “hello”
<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"> 
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

var x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
document.write(x.nodeValue);
x.replaceData(0,8,"Hello");

document.write("<br />");
document.write(x.nodeValue);

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

使用 nodeValue 属性

用 nodeValue 属性来替换文本节点中数据会更加容易。

下面的代码片段将用 “Easy Italian” 替换第一个 <title> 元素中的文本节点值:

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];

x.nodeValue="Hello World";

例子解释:

  • 通过使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中
  • 获取第一个 <title> 元素节点的文本节点
  • 使用 nodeValue 属性来更改这个文本节点的文本
<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"> 
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
document.write(x.nodeValue);
x.nodeValue="Hello World";

document.write("<br />");
document.write(x.nodeValue);

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

您可以在 改变节点 这一节中阅读更多有关更改节点值的内容。

作者:terry,如若转载,请注明出处:https://www.web176.com/xmldom/10829.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年2月27日
下一篇 2023年2月27日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注