I have a node of DOM document. How can I remove all of its child nodes? For example:
<employee>
<one/>
<two/>
<three/>
</employee>
Becomes:
<employee>
</employee>
I want to remove all child nodes of employee.
I have a node of DOM document. How can I remove all of its child nodes? For example:
<employee>
<one/>
<two/>
<three/>
</employee>
Becomes:
<employee>
</employee>
I want to remove all child nodes of employee.
Just use:
Node result = node.cloneNode(false);
As document:
Node cloneNode(boolean deep)
deep - If true, recursively clone the subtree under the specified node; if false, clone only the node itself (and its attributes, if it is an Element).
public static void removeAllChildren(Node node) {
NodeList nodeList = node.getChildNodes();
int i = 0;
do {
Node item = nodeList.item(i);
if (item.hasChildNodes()) {
removeAllChildren(item);
i--;
}
node.removeChild(item);
i++;
} while (i < nodeList.getLength());
}