title: JAX-WS trouble at handling the response message on the client side
body: <p>I am having some trouble with a client side implementation of a webservice. I have the wsdl and I have created the classes with the wsimport tool. </p>
  
  <p>When I invoke the webservice via these classes I can see the request and and response in the log... and it looks ok. 
  I can't use the classes to manipulate with the recieved data from the WS because the result object has no definition about the fields it contains... </p>
  
  <p>Because of that I was trying to get the raw response from the WebService... I did this by attaching a SoapHandler:</p>
  
  <pre><code>BindingProvider bindingProvider = (BindingProvider)port;
  List&lt;Handler&gt; handlerChain = new ArrayList&lt;Handler&gt;();
  handlerChain.add(new SoapMsgHandler());
  bindingProvider.getBinding().setHandlerChain(handlerChain);
  
  result = port.getXmlFromWS();
  ....
  </code></pre>
  
  <p>In the handler I implemented the handleMessage(SOAPMessageContext context) method to print the message to the log but I have encountered a problem...</p>
  
  <p>Below is a piece of the Handler class that should handle the response... but I get an exception at this line: SOAPMessage message = smc.getMessage()</p>
  
  <p>The exception is the following:</p>
  
  <pre><code>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(Unknown Source)
      at org.apache.xerces.dom.ParentNode.insertBefore(Unknown Source)
      at org.apache.xerces.dom.CoreDocumentImpl.insertBefore(Unknown Source)
      at org.apache.xerces.dom.NodeImpl.appendChild(Unknown Source)
      at org.jboss.ws.core.soap.SOAPDocument.appendChild(SOAPDocument.java:222)
      at org.jboss.ws.core.soap.SOAPPartImpl.appendChild(SOAPPartImpl.java:297)
      at com.sun.xml.bind.marshaller.SAX2DOMEx.startElement(SAX2DOMEx.java:176)
      at com.sun.xml.ws.encoding.TagInfoset.writeStart(TagInfoset.java:168)
      at com.sun.xml.ws.message.stream.StreamMessage.writeTo(StreamMessage.java:442)
      at com.sun.xml.ws.message.AbstractMessageImpl.readAsSOAPMessage(AbstractMessageImpl.java:193)
      at com.sun.xml.ws.handler.SOAPMessageContextImpl.getMessage(SOAPMessageContextImpl.java:79) 
  </code></pre>
  
  <p>Part of the handler implementation:</p>
  
  <pre><code>import javax.xml.soap.SOAPMessage;
  import javax.xml.ws.handler.MessageContext;
  import javax.xml.ws.handler.soap.SOAPHandler;
  import javax.xml.ws.handler.soap.SOAPMessageContext;
  
  public class SoapMsgHandler implements SOAPHandler&lt;SOAPMessageContext&gt; {
    @Override
    public boolean handleMessage(SOAPMessageContext context) {
      printTheMsg(context);
      return true;
    }
  
    private void printTheMsg(SOAPMessageContext smc) {
     SOAPMessage message = smc.getMessage();  //this line produces the mentioned exception
    ....
    }
    ....
   }
  </code></pre>
  
  <p>Any help about what might be wrong will be very appreciated. </p>