How to create a xml SOAP request in Java and read the response received

Viewed 52

I have to send a Soap Request like shown below and receive a response.

I need help in:

  1. Changing SOAP-ENV to soap.
  2. Changing the 3 urls present in Envelope
  3. I need help in sending this xml to my url.
  4. 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.

This is my request:

<soap:Envelope xmlns:soap="http://www.h3.org/2003/05/soap-envelope" xmlns:app="http://app.omni.neweg.com" xmlns:xsd="http://data.omni.neweg.com/xsd">
   <soap:Header/>
   <soap:Body>
      <app:wfUploadWorkitem>
         <!--Optional:-->
         <app:args0>
            <!--Optional:-->
            <xsd:password>***********</xsd:password>
            <!--Optional:-->
            <xsd:userName>**********</xsd:userName>
            <!--Optional:-->
            <xsd:address1>A25</xsd:address1>
            <!--Optional:-->
            <xsd:address2>New Delhi</xsd:address2>
            <!--Optional:-->
            <xsd:address3>Delhi</xsd:address3>
            <!--Optional:-->
            <xsd:agreement_tc>Yes</xsd:agreement_tc>
            
         </app:args0>
      </app:wfUploadWorkitem>
   </soap:Body>
</soap:Envelope>

This is my response:

<soapenv:Envelope xmlns:soapenv="http://www.h3.org/2003/05/soap-envelope">
   <soapenv:Header/>
   <soapenv:Body>
      <ns:wfUploadWorkitemResponse xmlns:ns="http://app.omni.neweg.com">
         <ns:return xsi:type="ax233:WFUploadWorkitemResponse" xmlns:ax233="http://data.omni.neweg.com/xsd" xmlns:ax235="http://excp.omni.neweg.com/xsd" xmlns:ax236="http://rmi.java/xsd" xmlns:ax237="http://io.java/xsd" xmlns:xsi="http://www.h3.org/2001/XMLSchema-instance">
            <ax233:txnStatus xsi:type="ax233:WFStatus">
               <ax233:description>GCL-0000000070-Workflow</ax233:description>
               <ax233:errorType xsi:nil="true"/>
               <ax233:mainCode>0</ax233:mainCode>
               <ax233:subCode xsi:nil="true"/>
               <ax233:subject xsi:nil="true"/>
            </ax233:txnStatus>
            <ax233:folderIndex>44926240</ax233:folderIndex>
            <ax233:processInstanceId>GCL-0000000070-Workflow</ax233:processInstanceId>
         </ns:return>
      </ns:wfUploadWorkitemResponse>
   </soapenv:Body>
</soapenv:Envelope>

So far I was able to find this sample code here and tried to customize it:

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("");

        SOAPBodyElement element0 = body.addBodyElement(envelope.createName("address1", "xsd", ""));
        element.addTextNode("A25");

        SOAPBodyElement element1 = body.addBodyElement(envelope.createName("address2", "xsd", ""));
        element1.addTextNode("New Delhi");


        SOAPBodyElement rootElement1 = body.addBodyElement(envelope.createName("wfUploadWorkitem","app",""));
        SOAPBodyElement rootElement2 = body.addBodyElement(envelope.createName("args0","app",""));
        rootElement1.addChildElement(rootElement2);
        rootElement2.addChildElement(element0);
        rootElement2.addChildElement(element1);

        soapMsg.writeTo(System.out);

        FileOutputStream fOut = new FileOutputStream("SoapMessage.xml");
        soapMsg.writeTo(fOut);

        System.out.println();
        System.out.println("SOAP msg created");

    }catch(Exception e){
        e.printStackTrace();
    } 

Any help is very much appreciated.

0 Answers
Related