Dom-xml-dom-replace-node

提供:Dev Guides
移動先:案内検索

XML DOM-ノードの置換

この章では、XML DOMオブジェクトのノードの置換操作について学習します。 知っているように、DOMのすべてはノードと呼ばれる階層的な情報単位で維持され、置換ノードはこれらの指定されたノードまたはテキストノードを更新する別の方法を提供します。

以下は、ノードを置き換える2つの方法です。

  • replaceChild()
  • replaceData()

replaceChild()

メソッド_replaceChild()_は、指定されたノードを新しいノードに置き換えます。

構文

insertData()の構文は次のとおりです-

Node replaceChild(Node newChild, Node oldChild) throws DOMException

どこで、

  • newChild-は、子リストに追加する新しいノードです。
  • oldChild-は、リスト内で置換されるノードです。
  • このメソッドは、置換されたノードを返します。

次の例(replacenode_example)は、XMLドキュメント(link:/dom/node.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として保存します(このファイルと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)は、XMLドキュメント(link:/dom/node.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_から開始して、新しいテキスト "9999999" に置き換えられます。 _5

実行

このファイルを_replacedata_example_としてサーバーパスに保存します(このファイルとnode.xmlはサーバーの同じパスにある必要があります)。 次のように出力を取得します-

ContactNo before replace operation: 1234567890

ContactNo after replace operation: 199999997890