DOMException encountered while deleting a node… here is a solution to the problem.
DOMException encountered while deleting a node
I get DOMException.HIERARCHY_REQUEST_ERR when navigating the document object to delete a specific node, after googling this error code, it says:
“HIERARCHY_REQUEST_ERR: If the type of this node does not allow a child node of the newChild node type, or if the node to be inserted is one of the ancestors of this node or the node itself, or if this node belongs to typed Document, the DOM application attempts to insert a second DocumentType or Element node
I checked the node type to be removed and both the Element and Text types cause exceptions
Here is my code
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(new ByteArrayInputStream(smil.getBytes()));
Element root = dom.getDocumentElement();
Node node = root.getFirstChild();
dom.removeChild(node);
Got started
org.w3c.dom.DOMException
at org.apache.harmony.xml.dom.InnerNodeImpl.removeChild(InnerNodeImpl.java:180)
Solution
I found a way to fix this situation
Change
dom.removeChild(node);
to
node.getParentNode().removeChild(node);
It seems to work for me 🙂