- 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删除节点操作。删除节点操作从文档中删除指定的节点。该操作可以实现删除文本节点、元素节点或属性节点等节点。
以下是用于删除节点操作的方法 -
移除子对象()
删除属性()
移除子对象()
方法removeChild()从子节点列表中删除oldChild指示的子节点,并返回它。删除子节点相当于删除文本节点。因此,删除子节点也会删除与其关联的文本节点。
句法
使用removeChild()的语法如下:
Node removeChild(Node oldChild) throws DOMException
在哪里,
oldChild - 是被删除的节点。
此方法返回删除的节点。
示例 - 删除当前节点
以下示例 (removecurrentnode_example.htm) 将 XML 文档 ( node.xml ) 解析为 XML DOM 对象,并从父节点中删除指定的节点 <ContactNo>。
<!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>
在上面的例子中 -
x = xmlDoc.getElementsByTagName("ContactNo")[0]获取索引为 0 的元素 <ContactNo>。
x.parentNode.removeChild(x); 从父节点中删除索引为 0 的元素 <ContactNo>。
执行
将此文件保存为服务器路径上的removecurrentnode_example.htm (此文件和node.xml 应位于服务器中的同一路径上)。我们得到以下结果 -
Before remove operation, total ContactNo elements: 3 After remove operation, total ContactNo elements: 2
示例 - 删除文本节点
以下示例 (removetextNode_example.htm) 将 XML 文档 ( node.xml ) 解析为 XML DOM 对象并删除指定的子节点 <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>
在上面的例子中 -
x = xmlDoc.getElementsByTagName("名字")[0]; − 获取索引为 0 的x的第一个元素 <FirstName> 。
y = x.childNodes[0]; − 在这一行中y保存要删除的子节点。
x.removeChild(y); − 删除指定的子节点。
执行
将此文件保存为服务器路径上的removetextNode_example.htm (此文件和node.xml 应位于服务器中的同一路径上)。我们得到以下结果 -
Text node of child node before removal is: 1 Text node of child node after removal is: 0
删除属性()
方法removeAttribute() 按名称删除元素的属性。
句法
使用removeAttribute()的语法如下:
void removeAttribute(java.lang.String name) throws DOMException
在哪里,
name - 是要删除的属性的名称。
例子
以下示例 (removeelementattribute_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('Employee');
document.write(x[1].getAttribute('category'));
document.write("<br>");
x[1].removeAttribute('category');
document.write(x[1].getAttribute('category'));
</script>
</body>
</html>
在上面的例子中 -
document.write(x[1].getAttribute('类别')); −调用第一个位置索引的属性类别值。
x[1].removeAttribute('类别'); − 删除属性值。
执行
将此文件保存为服务器路径上的removeelementattribute_example.htm (此文件和node.xml 应位于服务器中的同一路径上)。我们得到以下结果 -
Non-Technical null