I\'m building a script which has to patch XML files, including replacing one list of elements with another. The following function applies a patch (involving a possibly empt
Building on the diagnosis by @Maurice and @fahd...
Can't you just put a condition before
parent.removeChild(node);
such as
if (parent.isSameNode(node.getParentNode()))
Then it would only remove a direct child of the given parent.
parent.removeChild(node) is throwing a NOT_FOUND_ERR because node is not a child of parent. I see that node comes from getElementsByTagName which might not be an immediate child of parent. It could be anywhere under parent.
how about
nodeToBeRemoved.getParentNode().removeChild(nodeToBeRemoved);
This is because when you are doing parent.removeChild(node), parent is not necessarily the parent of the node because getElementsByTagName() is doing a recursive search.