You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Hervé Guidetti <he...@idbsoft.ch> on 2001/01/10 18:28:25 UTC

org.w3c.dom.DOMException : How to append a node to another document

 Hi,

 I am a neebie with DOM. So excuse for the question.

 I have two documents. I want to copy a node from the document 1
(theNewNode)
 and append it to a node of the document 2 (nodeToChange).

 I tried this :
  nodeToChange.appendChild(theNewNode);

 but I get "org.w3c.dom.DOMException: DOM005 Wrong document"

 Thanks for your time

 Hervé



Re: org.w3c.dom.DOMException : How to append a node to another document

Posted by Sebastien Ponce <se...@cern.ch>.
see the code below where node is copied under parent :

    Node newNode;
    if (parent.getOwnerDocument() == node.getOwnerDocument()) {
      // if the parent is in the same document as the node to copy
      // just duplicates the node here
      newNode = node.cloneNode(true);
    } else {
      // else the node does not belong to the same document
      // as its parent
      // Thus, imports the node into the new document
      newNode = parent.getOwnerDocument().importNode(node, true);
    }
    // finally put the new node in the parent
    parent.getNode().appendChild(newNode);

Seb

Hervé Guidetti wrote:

>  Hi,
>
>  I am a neebie with DOM. So excuse for the question.
>
>  I have two documents. I want to copy a node from the document 1
> (theNewNode)
>  and append it to a node of the document 2 (nodeToChange).
>
>  I tried this :
>   nodeToChange.appendChild(theNewNode);
>
>  but I get "org.w3c.dom.DOMException: DOM005 Wrong document"
>
>  Thanks for your time
>
>  Hervé
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org


Re: org.w3c.dom.DOMException : How to append a node to another document

Posted by Ian Roberts <ir...@decisionsoft.com>.
On Wed, 10 Jan 2001, Hervé Guidetti wrote:

>  Hi,
> 
>  I am a neebie with DOM. So excuse for the question.
> 
>  I have two documents. I want to copy a node from the document 1
> (theNewNode)
>  and append it to a node of the document 2 (nodeToChange).
> 
>  I tried this :
>   nodeToChange.appendChild(theNewNode);
> 
>  but I get "org.w3c.dom.DOMException: DOM005 Wrong document"

You need to do

nodeToChange.apendChild(document2.importNode(theNewNode, true));

Ian

-- 
Ian Roberts                     | irr@decisionsoft.com
DecisionSoft Ltd.               | http://www.decisionsoft.com/