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

5

59

I'm getting an error when I try to generate an element that should look like

<hold/>

Using the java client code generated by Axis2. java snippet

HoldPayment hold = new HoldPayment()
cr.setHold(hold);

but when I fire it off I get an error of WRONG_DOCUMENT_ERR. If I don't include this element in the message being fired off it works fine. anyone got any idea how to fix it?

AxisFault
 faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
 faultSubcode:
 faultString: org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
 faultActor:
 faultNode:
 faultDetail:
        {http://xml.apache.org/axis/}stackTrace:org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
        at com.sun.org.apache.xerces.internal.dom.ParentNode.internalInsertBefore(ParentNode.java:352)
        at com.sun.org.apache.xerces.internal.dom.ParentNode.insertBefore(ParentNode.java:284)
        at com.sun.org.apache.xerces.internal.dom.NodeImpl.appendChild(NodeImpl.java:235)
        at org.apache.axis.message.SOAPFaultBuilder.onEndChild(SOAPFaultBuilder.java:305)
        at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1090)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1774)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2930)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
        at javax.xml.parsers.SAXParser.parse(SAXParser.java:395)
        at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
        at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
        at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
        at org.apache.axis.transport.http.HTTPSender.readFromSocket(HTTPSender.java:796)
        at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:144)
        at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
        at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
        at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
        at org.apache.axis.client.AxisClient.invoke(AxisClient.java:165)
        at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
        at org.apache.axis.client.Call.invoke(Call.java:2767)
        at org.apache.axis.client.Call.invoke(Call.java:2443)
        at org.apache.axis.client.Call.invoke(Call.java:2366)
        at org.apache.axis.client.Call.invoke(Call.java:1812)
        at au.com.virginblue.www.schema._2005._02.booking_wsdl.BookingBindingStub.createReservation(BookingBindingStub.java:1803)
        at com.test.GetDJBooking.main(GetDJBooking.java:215)

        {http://xml.apache.org/axis/}hostname:test.com.au

org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
        at org.apache.axis.AxisFault.makeFault(AxisFault.java:101)
        at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:701)
        at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
        at org.apache.axis.transport.http.HTTPSender.readFromSocket(HTTPSender.java:796)
        at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:144)
        at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
        at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
        at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
        at org.apache.axis.client.AxisClient.invoke(AxisClient.java:165)
        at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
        at org.apache.axis.client.Call.invoke(Call.java:2767)
        at org.apache.axis.client.Call.invoke(Call.java:2443)
        at org.apache.axis.client.Call.invoke(Call.java:2366)
        at org.apache.axis.client.Call.invoke(Call.java:1812)
        at au.com.virginblue.www.schema._2005._02.booking_wsdl.BookingBindingStub.createReservation(BookingBindingStub.java:1803)
        at com.nhh.dj.GetDJBooking.main(GetDJBooking.java:215)
Caused by: org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
        at com.sun.org.apache.xerces.internal.dom.ParentNode.internalInsertBefore(ParentNode.java:352)
        at com.sun.org.apache.xerces.internal.dom.ParentNode.insertBefore(ParentNode.java:284)
        at com.sun.org.apache.xerces.internal.dom.NodeImpl.appendChild(NodeImpl.java:235)
        at org.apache.axis.message.SOAPFaultBuilder.onEndChild(SOAPFaultBuilder.java:305)
        at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1090)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1774)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2930)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
        at javax.xml.parsers.SAXParser.parse(SAXParser.java:395)
        at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
        at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
        ... 14 more
Availability answered 6/7, 2010 at 7:22 Comment(0)
R
122

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.

Righteous answered 3/2, 2011 at 11:37 Comment(4)
If firstDoc is the document where you are taking the node from and the secondDoc is where you are going to use it, then you should use: secondDoc.importNode(firstDocsNode, true) although the word 'import' implies that.Gratt
Any idea what the boolean (in this case true) stands for?Noguchi
The true is for deep copy - @see Document.importNode(...)Unchristian
Wow man. This answer is like an elixir of life. Still, would request @Sudheer Palyam to make it a bit more understandable. It is a little bit confusing for many who are novices to XML DOM.Squirmy
G
9

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>
Guess answered 29/12, 2016 at 14:16 Comment(0)
A
5

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.

Aspersorium answered 6/7, 2010 at 7:30 Comment(3)
Sorry I'm not sure what you mean, I can't see where I copy and pasted a node from one document to another. I'm using the client code generated from Axis2 wsdl2java. It generated a method for adding the element yet it causes an error when I add it?Availability
What's your axis version? There's at least on reported issue here: issues.apache.org/jira/browse/AXIS-2705Aspersorium
I am using 1.4. I'll see if using 1.5 will fix it.Availability
S
0

The answer is in complete adherence to Sudheer Palyam's answer and seeks to add a bit of clarity to his answer.

In the example below, I have a Node 'node' which I have created somewhere else and is a part of different XML Document.

Secondly, I have a NodeList whose Nodes I want to append in the Node 'node' mentioned above.

So, I imported the nodes from NodeList one by one into the Document of Node 'node'. Once a node is imported, I appended it into Node 'node'.

Document ownerDocument = node.getOwnerDocument();
for(int count = 0; count < otherDocumentNodeChildNodes.getLength(); count++)
{
    Node child = otherDocumentNodeChildNodes.item(count);
    Node importedNode = ownerDocument.importNode(child, true);
    node.appendChild(importedNode);
}
Squirmy answered 19/2, 2021 at 7:8 Comment(0)
F
0

In my case this problem occured with old Spring ws security version. Try to upgrade spring-ws-security version. Upgrade for 3.1.8 or later for java 8 or 4.0.10 for java 11

    <dependency>
        <groupId>org.springframework.ws</groupId>
        <artifactId>spring-ws-security</artifactId>
        <version>3.1.8</version>
    </dependency>

Maven dependency : https://mvnrepository.com/artifact/org.springframework.ws/spring-ws-security

Bug : https://issues.apache.org/jira/browse/WSS-634

Flourish answered 28/2 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.