XML DOM - 替换节点
在本章中,我们将研究 XML DOM 对象中的替换节点操作。我们知道 DOM 中的所有内容都保存在一个称为节点的分层信息单元中,替换节点提供了另一种更新这些指定节点或文本节点的方法。
以下是替换节点的两种方法。
- replaceChild()
- replaceData()
replaceChild()
方法 replaceChild() 用新节点替换指定节点。
语法
insertData() 具有以下语法 −
Node replaceChild(Node newChild, Node oldChild) 抛出 DOMException
其中,
newChild − 是要放入子列表中的新节点。
oldChild − 是列表中被替换的节点。
此方法返回被替换的节点。
示例
以下示例 (replacenode_example.htm) 将 XML 文档 (node.xml) 解析为 XML DOM 对象,并将指定节点 <FirstName> 替换为新节点 <Name>。
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc(filename) {
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else // code for IE5 and IE6 {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}
</script>
</head>
<body>
<script>
xmlDoc = loadXMLDoc("/dom/node.xml");
x = xmlDoc.documentElement;
z = xmlDoc.getElementsByTagName("FirstName");
document.write("<b>Content of FirstName element before replace operation</b><br>");
for (i=0;i<z.length;i++) {
document.write(z[i].childNodes[0].nodeValue);
document.write("<br>");
}
//创建一个 Employee 元素、FirstName 元素和一个文本节点
newNode = xmlDoc.createElement("Employee");
newTitle = xmlDoc.createElement("Name");
newText = xmlDoc.createTextNode("MS Dhoni");
//将文本节点添加到 title 节点,
newTitle.appendChild(newText);
//将 title 节点添加到 book 节点
newNode.appendChild(newTitle);
y = xmlDoc.getElementsByTagName("Employee")[0]
//用新节点替换第一个 book 节点
x.replaceChild(newNode,y);
z = xmlDoc.getElementsByTagName("FirstName");
document.write("<b>Content of FirstName element after replace operation</b><br>");
for (i = 0;i<z.length;i++) {
document.write(z[i].childNodes[0].nodeValue);
document.write("<br>");
}
</script>
</body>
</html>
执行
将此文件作为 replacenode_example.htm 保存在服务器路径上(此文件和 node.xml 应位于服务器中的同一路径上)。我们将获得如下所示的输出 −
Content of FirstName element before replace operation Tanmay Taniya Tanisha Content of FirstName element after replace operation Taniya Tanisha
replaceData()
方法 replaceData() 将从指定的 16 位单元偏移量开始的字符替换为指定的字符串。
语法
replaceData() 具有以下语法 −
void replaceData(int offset, int count, java.lang.String arg) 抛出 DOMException
其中
offset − 是开始替换的偏移量。
count − 是要替换的 16 位单元数。如果偏移量和计数的总和超过长度,则将替换数据末尾的所有 16 位单元。
arg −必须用其替换范围的 DOMString。
示例
以下示例 (replacedata_example.htm) 将 XML 文档 (node.xml) 解析为 XML DOM 对象并替换它。
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc(filename) {
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else // code for IE5 and IE6 {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}
</script>
</head>
<body>
<script>
xmlDoc = loadXMLDoc("/dom/node.xml");
x = xmlDoc.getElementsByTagName("ContactNo")[0].childNodes[0];
document.write("<b>ContactNo before replace operation:</b> "+x.nodeValue);
x.replaceData(1,5,"9999999");
document.write("<br>");
document.write("<b>ContactNo after replace operation:</b> "+x.nodeValue);
</script>
</body>
</html>
在上面的例子中 −
x.replaceData(2,3,"999"); − 这里 x 保存了指定元素 <ContactNo> 的文本,该元素的文本被新文本 "9999999" 替换,从位置 1 开始直到长度 5。
执行
将此文件另存为 replacedata_example.htm 并保存在服务器路径上(此文件和 node.xml 应位于服务器中的同一路径上)。我们将获得如下所示的输出 −
ContactNo before replace operation: 1234567890 ContactNo after replace operation: 199999997890

