Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

encount DOMException when remove node

Tags:

java

android

i encount DOMException.HIERARCHY_REQUEST_ERR when navigate the Document object to remove a pecific node, and after google this error code, It says:

"HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to insert is one of this node's ancestors or this node itself, or if this node is of type Document and the DOM application attempts to insert a second DocumentType or Element node"

I have check the node type which i want to remove, both Element and Text type will cause the exception

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);

it goes

org.w3c.dom.DOMException

at org.apache.harmony.xml.dom.InnerNodeImpl.removeChild(InnerNodeImpl.java:180)
like image 433
Nail Avatar asked Feb 15 '26 08:02

Nail


1 Answers

I found one method can solve this situation

change

    dom.removeChild(node);

to

    node.getParentNode().removeChild(node);

it seems works for me :-)

like image 100
Nail Avatar answered Feb 17 '26 21:02

Nail



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!