org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it

Solution 1:

I use this implementation to add Node from one XML document to other.

Node firstDocImportedNode = firstDoc.importNode(secondDocsNode, true);
firstDocNode.appendChild(firstDocImportedNode );

See if this helps. Trick is just importing a Node to other Document, instead of directly appending.

Solution 2:

We saw this error when send SOAP message with CXF.

org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
at org.apache.xerces.dom.ParentNode.internalInsertBefore(ParentNode.java:351)
at org.apache.xerces.dom.ParentNode.insertBefore(ParentNode.java:283)
at org.apache.xerces.dom.CoreDocumentImpl.insertBefore(CoreDocumentImpl.java:393)
at org.apache.xerces.dom.NodeImpl.appendChild(NodeImpl.java:236)
at org.jboss.ws.core.soap.SOAPDocument.appendChild(SOAPDocument.java:226)
at org.jboss.ws.core.soap.SOAPPartImpl.appendChild(SOAPPartImpl.java:300)
at org.apache.cxf.staxutils.W3CDOMStreamWriter.setChild(W3CDOMStreamWriter.java:119) [cxf-common-utilities-2.5.4.jar:2.5.4]
at org.apache.cxf.staxutils.W3CDOMStreamWriter.newChild(W3CDOMStreamWriter.java:109) [cxf-common-utilities-2.5.4.jar:2.5.4]
at org.apache.cxf.staxutils.W3CDOMStreamWriter.writeStartElement(W3CDOMStreamWriter.java:137) [cxf-common-utilities-2.5.4.jar:2.5.4]
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor.writeSoapEnvelopeStart(SoapOutInterceptor.java:122) [cxf-rt-bindings-soap-2.4.6.jar:2.4.6]

Error was in SAAJOutInterceptor that has processed before SoapOutInterceptor. Saaj implementation was from JBoss. And it create document with another classloader. This document from Saaj break SoapOutInterceptor.

We solved this problem adding dependency to our module.

<dependency>
    <groupId>com.sun.xml.messaging.saaj</groupId>
    <artifactId>saaj-impl</artifactId>
    <version>1.3</version>
</dependency>

Solution 3:

You can't copy a node from one document and paste it to another. An attempt to do so results in this type of error.

The node has to be properly imported. If I remember well, the Document class offers the right methods for this common task.

EDIT

The problem might be in the code behind the setHold method. The element has to be imported. If this is all autogenerated code, then looking for a newer version of axis could solve the problem. See my comment, where I mentioned an axis 1.3/1.4 issue.