Dom-xml-dom-remove-node
XML DOM-ノードの削除
この章では、XML DOM _ノード削除_操作について学習します。 ノードの削除操作は、指定されたノードをドキュメントから削除します。 この操作は、テキストノード、要素ノード、属性ノードなどのノードを削除するために実装できます。
以下は、ノードの削除操作に使用されるメソッドです-
- removeChild()
- removeAttribute()
removeChild()
メソッド_removeChild()_は、_oldChild_で示される子ノードを子のリストから削除し、それを返します。 子ノードを削除することは、テキストノードを削除することと同じです。 したがって、子ノードを削除すると、それに関連付けられているテキストノードも削除されます。
構文
removeChild()を使用する構文は次のとおりです-
Node removeChild(Node oldChild) throws DOMException
どこで、
- oldChild-は削除されるノードです。
- このメソッドは、削除されたノードを返します。
例-現在のノードを削除
次の例(removecurrentnode_example)は、XMLドキュメント(link:/dom/node.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_としてサーバーパスに保存します(このファイルとnode.xmlはサーバーの同じパスにある必要があります)。 次の結果が得られます-
Before remove operation, total ContactNo elements: 3
After remove operation, total ContactNo elements: 2
例-テキストノードの削除
次の例(removetextNode_example)は、XMLドキュメント(link:/dom/node.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( "FirstName")[0]; _-0でインデックス付けされた_x_に最初の要素<FirstName>を取得します。
- _y = x.childNodes [0]; _-この行の_y_は、削除する子ノードを保持します。
- _x.removeChild(y); _-指定された子ノードを削除します。
実行
このファイルを_removetextNode_example_としてサーバーパスに保存します(このファイルと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
どこで、
- name-削除する属性の名前です。
例
次の例(removeelementattribute_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('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')); _-1番目の位置にインデックス付けされた属性_category_の値が呼び出されます。
- _x [1] .removeAttribute( 'category'); _-属性値を削除します。
実行
このファイルを_removeelementattribute_example_としてサーバーパスに保存します(このファイルとnode.xmlはサーバーの同じパスにある必要があります)。 次の結果が得られます-
Non-Technical
null