杨莹: > I get have two svg document instances. the docA and docB, I want to > insert docA to docB, because I know SVG tag can be nested. so I code > followling: > > Node node = docA.getFirstChild();// the svg root element > docB.appendChild(node); // nest svg
In this case you are lucky that the first child of the Document node is in fact the <svg> element. But this might not always be the case: you can have a Comment node before it, for example. It would be better to use Node node = docA.getDocumentElement(); instead. > but I run the code and get the exception: > > org.w3c.dom.DOMException: The node (type: 1, name: svg) cannot be > inserted, since the document node already has a node of type 1. > at > org.apache.batik.dom.AbstractNode.createDOMException(AbstractNode.java:408) > at > org.apache.batik.dom.AbstractDocument.checkChildType(AbstractDocument.java:855) > at > org.apache.batik.dom.AbstractParentNode.checkAndRemove(AbstractParentNode.java:455) > at > org.apache.batik.dom.AbstractParentNode.appendChild(AbstractParentNode.java:203) > > why that exception happens? how can I fix it? thanks for reply. It’s because you are trying to append the <svg> element as a child of the Document node, and a Document node can have only one Element child (which is what the “type 1” means). Probably you want to append the <svg> element as a child of the root <svg> in docB. Also, since ‘node’ comes from a different document, you need to call importNode() so that it can be placed in docB: // the ‘true’ means to perform a deep importation (the whole subtree) Node node = docB.importNode(docA.getDocumentElement(), true); docB.getDocumentElement().appendChild(node); -- Cameron McCormack ≝ http://mcc.id.au/ --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
