[ACCEPTED]-Remove all child nodes of a node-dom
Accepted answer
No need to remove child nodes of child nodes
public static void removeChilds(Node node) {
while (node.hasChildNodes())
node.removeChild(node.getFirstChild());
}
0
public static void removeAllChildren(Node node)
{
for (Node child; (child = node.getFirstChild()) != null; node.removeChild(child));
}
0
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).
0
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());
}
0
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.