doc.removeChild(node);
will not work because the node you're trying to remove isn't a child of the document node, it's a child of the document element (the a
), which itself is a child of the document root node. You need to call removeChild
on the correct parent node:
node.getParentNode().removeChild(node);