XML DOM - 删除节点

在本章中,我们将研究XML DOM 删除节点操作. remove节点操作从文档中删除指定的节点.可以实现此操作以删除节点,如文本节点,元素节点或属性节点.

以下是用于删除节点操作的方法 :

  • removeChild()

  • removeAttribute()

removeChild()

方法 removeChild()删除

语法

使用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("https://img01.yuandaxia.cn/Content/img/tutorials/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] 获取元素< ContactNo>索引为0.

  • x.parentNode.removeChild(x); 删除元素< ContactNo>从父节点索引为0.

执行

将此文件另存为服务器路径上的removecurrentnode_example.htm (此文件和node.xml应位于服务器的同一路径上).我们得到以下结果 :

 
在删除操作之前,总ContactNo元素:3 
删除操作后,总ContactNo元素: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("https://img01.yuandaxia.cn/Content/img/tutorials/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("FirstName")[0]; : 得到第一个元素< FirstName> x 索引为0.

  • 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()按名称删除元素的属性.

语法

使用 removeAttribute()的语法如下 :

void removeAttribute(java.lang.String name) throws DOMException

Where,

  • 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("https://img01.yuandaxia.cn/Content/img/tutorials/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('category')); : 调用在第一个位置索引的属性 category 的值.

  • x [1] .removeAttribute('category') ; : 删除属性值.

执行

将此文件另存为 removeelementattribute_example.htm 在服务器路径上(此文件和node.xml应位于服务器的同一路径上).我们得到以下结果 :

Non-Technical
null