title: How to create a xml SOAP request in Java and read the response received
body: <p>I have to send a Soap Request like shown below and receive a response.</p>
  <p>I need help in:</p>
  <ol>
  <li>Changing SOAP-ENV to soap.</li>
  <li>Changing the 3 urls present in Envelope</li>
  <li>I need help in sending this xml to my url.</li>
  <li>I need help in receiving and reading the xml data.
  I was able to partially make the request but i am still facing many issues.</li>
  </ol>
  <p>This is my request:</p>
  <pre><code>&lt;soap:Envelope xmlns:soap=&quot;http://www.h3.org/2003/05/soap-envelope&quot; xmlns:app=&quot;http://app.omni.neweg.com&quot; xmlns:xsd=&quot;http://data.omni.neweg.com/xsd&quot;&gt;
     &lt;soap:Header/&gt;
     &lt;soap:Body&gt;
        &lt;app:wfUploadWorkitem&gt;
           &lt;!--Optional:--&gt;
           &lt;app:args0&gt;
              &lt;!--Optional:--&gt;
              &lt;xsd:password&gt;***********&lt;/xsd:password&gt;
              &lt;!--Optional:--&gt;
              &lt;xsd:userName&gt;**********&lt;/xsd:userName&gt;
              &lt;!--Optional:--&gt;
              &lt;xsd:address1&gt;A25&lt;/xsd:address1&gt;
              &lt;!--Optional:--&gt;
              &lt;xsd:address2&gt;New Delhi&lt;/xsd:address2&gt;
              &lt;!--Optional:--&gt;
              &lt;xsd:address3&gt;Delhi&lt;/xsd:address3&gt;
              &lt;!--Optional:--&gt;
              &lt;xsd:agreement_tc&gt;Yes&lt;/xsd:agreement_tc&gt;
              
           &lt;/app:args0&gt;
        &lt;/app:wfUploadWorkitem&gt;
     &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  <p>This is my response:</p>
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv=&quot;http://www.h3.org/2003/05/soap-envelope&quot;&gt;
     &lt;soapenv:Header/&gt;
     &lt;soapenv:Body&gt;
        &lt;ns:wfUploadWorkitemResponse xmlns:ns=&quot;http://app.omni.neweg.com&quot;&gt;
           &lt;ns:return xsi:type=&quot;ax233:WFUploadWorkitemResponse&quot; xmlns:ax233=&quot;http://data.omni.neweg.com/xsd&quot; xmlns:ax235=&quot;http://excp.omni.neweg.com/xsd&quot; xmlns:ax236=&quot;http://rmi.java/xsd&quot; xmlns:ax237=&quot;http://io.java/xsd&quot; xmlns:xsi=&quot;http://www.h3.org/2001/XMLSchema-instance&quot;&gt;
              &lt;ax233:txnStatus xsi:type=&quot;ax233:WFStatus&quot;&gt;
                 &lt;ax233:description&gt;GCL-0000000070-Workflow&lt;/ax233:description&gt;
                 &lt;ax233:errorType xsi:nil=&quot;true&quot;/&gt;
                 &lt;ax233:mainCode&gt;0&lt;/ax233:mainCode&gt;
                 &lt;ax233:subCode xsi:nil=&quot;true&quot;/&gt;
                 &lt;ax233:subject xsi:nil=&quot;true&quot;/&gt;
              &lt;/ax233:txnStatus&gt;
              &lt;ax233:folderIndex&gt;44926240&lt;/ax233:folderIndex&gt;
              &lt;ax233:processInstanceId&gt;GCL-0000000070-Workflow&lt;/ax233:processInstanceId&gt;
           &lt;/ns:return&gt;
        &lt;/ns:wfUploadWorkitemResponse&gt;
     &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  <p>So far I was able to find this sample code <a href="https://www.jitendrazaa.com/blog/java/create-soap-message-using-java/" rel="nofollow noreferrer">here</a> and tried to customize it:</p>
  <pre><code>try{
          MessageFactory factory = MessageFactory.newInstance();
          SOAPMessage soapMsg = factory.createMessage();
          SOAPPart part = soapMsg.getSOAPPart();
  
          SOAPEnvelope envelope = part.getEnvelope();
          SOAPHeader header = envelope.getHeader();
          SOAPBody body = envelope.getBody();
  
          header.addTextNode(&quot;&quot;);
  
          SOAPBodyElement element0 = body.addBodyElement(envelope.createName(&quot;address1&quot;, &quot;xsd&quot;, &quot;&quot;));
          element.addTextNode(&quot;A25&quot;);
  
          SOAPBodyElement element1 = body.addBodyElement(envelope.createName(&quot;address2&quot;, &quot;xsd&quot;, &quot;&quot;));
          element1.addTextNode(&quot;New Delhi&quot;);
  
  
          SOAPBodyElement rootElement1 = body.addBodyElement(envelope.createName(&quot;wfUploadWorkitem&quot;,&quot;app&quot;,&quot;&quot;));
          SOAPBodyElement rootElement2 = body.addBodyElement(envelope.createName(&quot;args0&quot;,&quot;app&quot;,&quot;&quot;));
          rootElement1.addChildElement(rootElement2);
          rootElement2.addChildElement(element0);
          rootElement2.addChildElement(element1);
  
          soapMsg.writeTo(System.out);
  
          FileOutputStream fOut = new FileOutputStream(&quot;SoapMessage.xml&quot;);
          soapMsg.writeTo(fOut);
  
          System.out.println();
          System.out.println(&quot;SOAP msg created&quot;);
  
      }catch(Exception e){
          e.printStackTrace();
      } 
  </code></pre>
  <p>Any help is very much appreciated.</p>