Dom 简明教程
XML DOM - Replace Node
在本章中,我们将学习 XML DOM 对象中的替换节点操作。众所周知,DOM 中的所有内容都保留在称为节点的层次信息单元中,替换节点提供了另一个方法来更新这些指定节点或文本节点。
In this chapter, we will study about the replace node operation in an XML DOM object. As we know everything in the DOM is maintained in a hierarchical informational unit known as node and the replacing node provides another way to update these specified nodes or a text node.
以下是替换节点的两种方法。
Following are the two methods to replace the nodes.
-
replaceChild()
-
replaceData()
replaceChild()
方法 replaceChild() 用新节点替换指定节点。
The method replaceChild() replaces the specified node with the new node.
Syntax
insertData() 具有以下语法:
The insertData() has the following syntax −
Node replaceChild(Node newChild, Node oldChild) throws DOMException
其中,
Where,
-
newChild − is the new node to put in the child list.
-
oldChild − is the node being replaced in the list.
-
This method returns the node replaced.
Example
以下示例 (replacenode_example.htm) 将 XML 文档 ( node.xml ) 解析为 XML DOM 对象,并用新节点 <Name> 替换指定节点 <FirstName>。
The following example (replacenode_example.htm) parses an XML document (node.xml) into an XML DOM object and replaces the specified node <FirstName> with the new node <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>
Execution
将此文件作为 replacenode_example.htm 保存到服务器路径中(此文件和 node.xml 应该在服务器中的同一路径中)。然后,我们将得到如下所示的输出:
Save this file as replacenode_example.htm on the server path (this file and node.xml should be on the same path in your server). We will get the output as shown below −
Content of FirstName element before replace operation
Tanmay
Taniya
Tanisha
Content of FirstName element after replace operation
Taniya
Tanisha
replaceData()
方法 replaceData() 用指定字符串替换从指定的 16 位单元偏移开始的字符。
The method replaceData() replaces the characters starting at the specified 16-bit unit offset with the specified string.
Syntax
replaceData() 的语法如下所示:
The replaceData() has the following syntax −
void replaceData(int offset, int count, java.lang.String arg) throws DOMException
其中
Where
-
offset − is the offset from which to start replacing.
-
count − is the number of 16-bit units to replace. If the sum of offset and count exceeds length, then all the 16-bit units to the end of the data are replaced.
-
arg − the DOMString with which the range must be replaced.
Example
以下示例 (replacedata_example.htm) 将 XML 文档 ( node.xml ) 解析成 XML DOM 对象并替换它。
The following example (replacedata_example.htm) parses an XML document (node.xml) into an XML DOM object and replaces it.
<!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>
在以上示例中 −
In the above example −
-
x.replaceData(2,3,"999"); − Here x holds the text of the specified element <ContactNo> whose text is replaced by the new text "9999999", starting from the position 1 till the length of 5.
Execution
将该文件另存为 replacedata_example.htm,保存到服务器路径中(该文件和 node.xml 应在服务器中位于同一路径下)。我们将得到如以下所示的输出:
Save this file as replacedata_example.htm on the server path (this file and node.xml should be on the same path in your server). We will get the output as shown below −
ContactNo before replace operation: 1234567890
ContactNo after replace operation: 199999997890