title: How do I append an XML request to the only SOAPElement child of the SOAP Body?
body: <h2>The use case</h2>
  
  <p>I need to contact an external SOAP service in a programmatic way. To do so, I need to create a SOAP request which looks like the below: </p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pric="http://myURI/"&gt;
     &lt;soapenv:Header/&gt;
     &lt;soapenv:Body&gt;
        &lt;pric:myAPI&gt;
          &lt;XmlDocument&gt;
              &lt;OtherXmlContent&gt;
              &lt;/OtherXmlContent&gt;
          &lt;/XmlDocument&gt;
        &lt;/pric:myAPI&gt;
     &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <h2>The task I'm blocked on</h2>
  
  <p>I manage  to create the following SOAP envelope: </p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pric="http://myURI/"&gt;
     &lt;soapenv:Header/&gt;
     &lt;soapenv:Body&gt;
        &lt;pric:myAPI&gt;
  
        &lt;/pric:myAPI&gt;
     &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>What I need to do, provided an input, is to add the XML request: </p>
  
  <pre><code>&lt;XmlDocument&gt;
      &lt;OtherXmlContent&gt;
      &lt;/OtherXmlContent&gt;
  &lt;/XmlDocument&gt;
  </code></pre>
  
  <p>... as a child of <code>&lt;pric:myAPI&gt;</code>, which is the only child of my <code>&lt;soapenv:Body&gt;</code>.</p>
  
  <p>For information, the above Soap envelope (without <code>XmlDocument</code> yet) is created by the following code: </p>
  
  <pre><code>MessageFactory messageFactory = MessageFactory.newInstance();
  SOAPMessage soapMessage = messageFactory.createMessage();
  SOAPPart soapPart = soapMessage.getSOAPPart();
  SOAPEnvelope envelope = soapPart.getEnvelope();
  envelope.addNamespaceDeclaration(pricingNamespace, pricingNamespaceURI);
  SOAPBody soapBody = envelope.getBody();
  SOAPElement pricingWrapper = soapBody.addChildElement(pricingAction, pricingNamespace);
  </code></pre>
  
  <p>... hence, what I need to do is to append a child to <code>pricingWrapper</code>. I choose how I create this child, I have full control on the function generating it which is this one: </p>
  
  <pre><code>private static String createXmlProductFromDealingDocument(Document dealings) 
  </code></pre>
  
  <h2>Attempt 1 - adding the document as text</h2>
  
  <p>I have tried to add the <code>XmlDocument</code> as text of <code>pricingWrapper</code>. 
  This is how I did it: </p>
  
  <pre><code>pricingWrapper.addTextNode(createXmlProductFromDealingDocument(dealingFile));
  </code></pre>
  
  <p>The problem, though, is that all the characters <code>&lt;</code> and <code>&gt;</code> of the <code>XmlDocument</code> rendered as <code>String</code> are escaped by the method <code>addTextNode</code>. In other words, I can see my body has the correct content, but <code>&lt;</code> is replaced with <code>&amp;lt;</code> and <code>&gt;</code> is replaced with <code>&amp;gt;</code>, so making the SOAP request invalid for the target service. </p>
  
  <h2>Attempt 2 - adding the document as child node</h2>
  
  <p>Another attempt I did was to return a <code>Node</code> instead of a <code>String</code> from my function: </p>
  
  <pre><code>private static Node createXmlProductFromDealingDocument(Document dealings) 
  </code></pre>
  
  <p>and append this <code>Node</code> as a child of <code>pricingWrapper</code>:</p>
  
  <pre><code>pricingWrapper.appendChild(createXmlProductFromDealingDocument(dealingFile));
  </code></pre>
  
  <p>The above raises an exception of type:</p>
  
  <blockquote>
    <p>org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.</p>
  </blockquote>
  
  <p>Ok, fair enough. I posted a question (deleted shortly as I wanted to go deeper in my researches) yesterday, and a user kindly suggested me in a comment to check how to clone DOM nodes by referencing <a href="https://stackoverflow.com/questions/873247/how-do-i-copy-dom-nodes-from-one-document-to-another-in-java">this answer</a>. </p>
  
  <p>I tried to do so as follows: </p>
  
  <pre><code>Node pricingRequest = createXmlProductFromDealingDocument(dealingFile);
  Node soapPricingRequest = pricingRequest.cloneNode(true);
  pricingWrapper.getOwnerDocument().adoptNode(soapPricingRequest);
  pricingWrapper.appendChild(soapPricingRequest);
  </code></pre>
  
  <p>However, this raises a new exception:</p>
  
  <blockquote>
    <p>org.w3c.dom.DOMException: NOT_SUPPORTED_ERR: The implementation does not support the requested type of object or operation.</p>
  </blockquote>
  
  <p>... on the following line: </p>
  
  <pre><code>pricingWrapper.getOwnerDocument().adoptNode(soapPricingRequest);
  </code></pre>
  
  <p>... and I really don't see how else I could append the child differently than what I did above. </p>
  
  <h2>What is my question</h2>
  
  <p>I just would like to complete my SOAP request the right way. I do not have any preference whether this would be done by injecting the XML as text or as a <code>Node</code>, as far as is the proper way and especially as far as it works :)
  Could anyone please tip me how could I fix my above issues? </p>