XML DOM - 替換節點



本章我們將學習在 XML DOM 物件中替換節點的操作。眾所周知,DOM 中的一切都以稱為節點的分層資訊單元維護,而替換節點提供了另一種更新這些指定節點或文字節點的方法。

以下是替換節點的兩種方法。

  • replaceChild()
  • replaceData()

replaceChild()

方法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()

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> 的文字,其文字從位置 1 開始到長度 5 被新文字 "9999999" 替換。

執行

將此檔案另存為 replacedata_example.htm 到伺服器路徑(此檔案和 node.xml 應位於伺服器上的同一路徑)。我們將得到如下所示的輸出:

ContactNo before replace operation: 1234567890

ContactNo after replace operation: 199999997890 
廣告