Dom 简明教程
XML DOM - Remove Node
在本章中,我们将学习 XML DOM 删除节点操作。删除节点操作会从文档中删除指定的节点。可以实现此操作删除节点,如文本节点、元素节点或属性节点。
In this chapter, we will study about the XML DOM Remove Node operation. The remove node operation removes the specified node from the document. This operation can be implemented to remove the nodes like text node, element node or an attribute node.
以下是用于执行删除节点操作的方法 -
Following are the methods that are used for remove node operation −
-
removeChild()
-
removeAttribute()
removeChild()
该方法 removeChild() 从子项列表中删除由 oldChild 指示的子项,并返回它。删除子项等效于删除文本节点。因此,删除子项会删除与其关联的文本节点。
The method removeChild() removes the child node indicated by oldChild from the list of children, and returns it. Removing a child node is equivalent to removing a text node. Hence, removing a child node removes the text node associated with it.
Syntax
使用 removeChild() 的语法如下 -
The syntax to use removeChild() is as follows −
Node removeChild(Node oldChild) throws DOMException
其中,
Where,
-
oldChild − is the node being removed.
-
This method returns the node removed.
Example - Remove Current Node
下列示例 (removecurrentnode_example.htm) 将 XML 文档 ( node.xml ) 解析到 XML DOM 对象,并从父节点中删除指定的节点 <ContactNo>。
The following example (removecurrentnode_example.htm) parses an XML document (node.xml) into an XML DOM object and removes the specified node <ContactNo> from the parent node.
<!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");
document.write("<b>Before remove operation, total ContactNo elements: </b>");
document.write(xmlDoc.getElementsByTagName("ContactNo").length);
document.write("<br>");
x = xmlDoc.getElementsByTagName("ContactNo")[0];
x.parentNode.removeChild(x);
document.write("<b>After remove operation, total ContactNo elements: </b>");
document.write(xmlDoc.getElementsByTagName("ContactNo").length);
</script>
</body>
</html>
在以上示例中 −
In the above example −
-
x = xmlDoc.getElementsByTagName("ContactNo")[0] gets the element <ContactNo> indexed at 0.
-
x.parentNode.removeChild(x); removes the element <ContactNo> indexed at 0 from the parent node.
Execution
在服务器路径上将此文件保存为 removecurrentnode_example.htm(此文件和 node.xml 应在服务器中的同一路径)。我们得到以下结果 -
Save this file as removecurrentnode_example.htm on the server path (this file and node.xml should be on the same path in your server). We get the following result −
Before remove operation, total ContactNo elements: 3
After remove operation, total ContactNo elements: 2
Example - Remove Text Node
以下示例 (removetextNode_example.htm) 将 XML 文档 ( node.xml ) 解析成一个 XML DOM 对象并将指定的子节点 <FirstName> 移除。
The following example (removetextNode_example.htm) parses an XML document (node.xml) into an XML DOM object and removes the specified child node <FirstName>.
<!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("FirstName")[0];
document.write("<b>Text node of child node before removal is:</b> ");
document.write(x.childNodes.length);
document.write("<br>");
y = x.childNodes[0];
x.removeChild(y);
document.write("<b>Text node of child node after removal is:</b> ");
document.write(x.childNodes.length);
</script>
</body>
</html>
在以上示例中 −
In the above example −
-
x = xmlDoc.getElementsByTagName("FirstName")[0]; − gets the first element <FirstName> to the x indexed at 0.
-
y = x.childNodes[0]; − in this line y holds the child node to be remove.
-
x.removeChild(y); − removes the specified child node.
Execution
在服务器的路径中将此文件另存为 removetextNode_example.htm(此文件和 node.xml 应该在服务器的同一路径中)。我们获得以下结果 -
Save this file as removetextNode_example.htm on the server path (this file and node.xml should be on the same path in your server). We get the following result −
Text node of child node before removal is: 1
Text node of child node after removal is: 0
removeAttribute()
removeAttribute() 方法通过名称移除元素的一个属性。
The method removeAttribute() removes an attribute of an element by name.
Syntax
removeAttribute() 的语法用法如下 -
Syntax to use removeAttribute() is as follows −
void removeAttribute(java.lang.String name) throws DOMException
其中,
Where,
-
name − is the name of the attribute to remove.
Example
以下示例 (removeelementattribute_example.htm) 将 XML 文档 ( node.xml ) 解析到 XML DOM 对象中并移除指定的属性节点。
The following example (removeelementattribute_example.htm) parses an XML document (node.xml) into an XML DOM object and removes the specified attribute node.
<!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('Employee');
document.write(x[1].getAttribute('category'));
document.write("<br>");
x[1].removeAttribute('category');
document.write(x[1].getAttribute('category'));
</script>
</body>
</html>
在以上示例中 −
In the above example −
-
document.write(x[1].getAttribute('category')); − value of attribute category indexed at 1st position is invoked.
-
x[1].removeAttribute('category'); − removes the attribute value.