Dark Soul Dark Soul - 1 month ago 15
Java Question

How to request from a SOAP webservice, convert the SOAP response to XML and compare it to another SOAP response, in JAVA

i am completely new in doing this 3 steps, so can you please help me step by step. (I understand Java language, did couple of scripts here and there but never touched SOAP stuff).
I need to do this:

1) Request from two SOAP services and store the responses in two objects.

2) Transform the response in XML(maybe, maybe not, depends if the output is in the form
< tag>< /tag>
then no transformation required, but if it is
< n32:tag>< n32:tag>
then i will want to get rid of "n32".

3) Compare those two responses and see where is the difference at node/tag and maybe inside tag level (maybe using XMLUnit)

4) Report the differences, in console.(not as an error in JUnit).


  1. Since you have the webservice endpoint, I sugest you to create webservice clients for each service.

You can do it with wsimport, that already comes with JDK:

wsimport.bat -d "D:\WS" -keep -verbose endpoint_ws.wsdl

After this command, you will have java objects to access the webservice.

Put these objects in your project and access the webservice.

Here a reference on how to do:


  1. Now that you have objects of the responses, you could code and compare each property. If there's a need to transform these objects into xml again for comparison (I say again because the SOAP message is already xml), you could use xstream (http://x-stream.github.io/tutorial.html).


If you don't need to deal with the java client objects, you could follow this post:

How to do a SOAP Web Service call from Java class?

In the second part of the post, is shown how to interact directly with the request/response messages:

import javax.xml.soap.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class SOAPClientSAAJ {

     * Starting point for the SAAJ - SOAP Client Testing
    public static void main(String args[]) {
        try {
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server
            String url = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

            // Process the SOAP Response

        } catch (Exception e) {
            System.err.println("Error occurred while sending SOAP Request to Server");

    private static SOAPMessage createSOAPRequest() throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String serverURI = "http://ws.cdyne.com/";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("example", serverURI);

        Constructed SOAP Request Message:
        <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ws.cdyne.com/">

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example");
        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example");
        SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey", "example");

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", serverURI  + "VerifyEmail");


        /* Print the request message */
        System.out.print("Request SOAP Message = ");

        return soapMessage;

     * Method used to print the SOAP Response
    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source sourceContent = soapResponse.getSOAPPart().getContent();
        System.out.print("\nResponse SOAP Message = ");
        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);



To create a soap message directly from a string, first create a InputStream

InputStream is = new ByteArrayInputStream(send.getBytes());
SOAPMessage request = MessageFactory.newInstance().createMessage(null, is);

More info:

convert string to soap in java