You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by 杨莹 <y_...@163.com> on 2009/03/08 16:47:33 UTC

appendChild exception!

batik-users,您好!

	   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
           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.
   
                                Give my best wishes to you
                                       Yours sincerely 		
        杨莹
        y_m1215@163.com
        2009-03-08

Re: appendChild exception!

Posted by Cameron McCormack <ca...@mcc.id.au>.
杨莹:
> 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: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org