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
.
Child nodes can be removed from a parent with removeChild(), and a node itself can be removed with remove(). Another method to remove all child of a node is to set it's innerHTML=”” property, it is an empty string which produces the same output.
The empty() method removes all child nodes from the set of matched elements.
No need to remove child nodes of child nodes
public static void removeChilds(Node node) {
while (node.hasChildNodes())
node.removeChild(node.getFirstChild());
}
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With