- XML DOM 基础知识
- XML DOM - 主页
- XML DOM - 概述
- XML DOM - 模型
- XML DOM - 节点
- XML DOM - 节点树
- XML DOM - 方法
- XML DOM - 加载
- XML DOM - 遍历
- XML DOM - 导航
- XML DOM - 访问
- XML DOM 操作
- XML DOM - 获取节点
- XML DOM - 设置节点
- XML DOM - 创建节点
- XML DOM - 添加节点
- XML DOM - 替换节点
- XML DOM - 删除节点
- XML DOM - 克隆节点
- XML DOM 对象
- DOM - 节点对象
- DOM - 节点列表对象
- DOM - 命名节点映射对象
- DOM - DOMI 实现
- DOM - 文档类型对象
- DOM - 处理指令
- DOM-实体对象
- DOM - 实体引用对象
- DOM - 表示法对象
- DOM - 元素对象
- DOM - 属性对象
- DOM - CDATASection 对象
- DOM - 评论对象
- DOM - XMLHttpRequest 对象
- DOM - DOMException 对象
- XML DOM 有用的资源
- XML DOM - 快速指南
- XML DOM - 有用的资源
- XML DOM - 讨论
XML DOM - 替换节点
在本章中,我们将研究 XML DOM 对象中的替换节点操作。正如我们所知,DOM 中的所有内容都维护在称为节点的分层信息单元中,并且替换节点提供了另一种更新这些指定节点或文本节点的方法。
以下是替换节点的两种方法。
- 替换子对象()
- 替换数据()
替换子对象()
ReplaceChild()方法用新节点替换指定节点。
句法
insertData() 具有以下语法 -
Node replaceChild(Node newChild, Node oldChild) throws 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>");
}
//create a Employee element, FirstName element and a text node
newNode = xmlDoc.createElement("Employee");
newTitle = xmlDoc.createElement("Name");
newText = xmlDoc.createTextNode("MS Dhoni");
//add the text node to the title node,
newTitle.appendChild(newText);
//add the title node to the book node
newNode.appendChild(newTitle);
y = xmlDoc.getElementsByTagName("Employee")[0]
//replace the first book node with the new node
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() 方法用指定的字符串替换从指定的 16 位单位偏移量开始的字符。
句法
ReplaceData() 具有以下语法 -
void replaceData(int offset, int count, java.lang.String arg) throws 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