开发者

encount DOMException when remove node

开发者 https://www.devze.com 2023-04-06 21:37 出处:网络
i encount DOMException.HIERARCHY_REQUEST_ERR when navigate the Document object to remove a pecific node, and after google this error code, It says:

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开发者_C百科.xml.dom.InnerNodeImpl.removeChild(InnerNodeImpl.java:180)


I found one method can solve this situation

change

    dom.removeChild(node);

to

    node.getParentNode().removeChild(node);

it seems works for me :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号