How to create SOAP request with CDATA

LMK picture LMK · Jun 4, 2014 · Viewed 19.4k times · Source

I am new to SOAP,I want to create SOAP request,as given below

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
 <SOAP-ENV:Header/>
<soapenv:Body>
  <tem:RequestData>
     <tem:requestDocument>
        <![CDATA[
        <Request>
           <Authentication CMId="68" Function="1" Guid="5594FB83-F4D4-431F-B3C5-EA6D7A8BA795" Password="poihg321TR"/>
           <Establishment Id="4297867"/>
        </Request>
        ]]>

     </tem:requestDocument>
  </tem:RequestData>
</soapenv:Body>
</soapenv:Envelope>  

Code for creating SOAP request i have found in tutorial

  MessageFactory mf = MessageFactory.newInstance();
                    SOAPMessage sm = mf.createMessage();
                    SOAPEnvelope envelope = sm.getSOAPPart().getEnvelope();
                    envelope.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelope/");
                    envelope.setPrefix("soapenv");

                    envelope.setAttribute("xmlns:tem", "http://tempuri.org/");       

                    SOAPBody body = envelope.getBody();
                    body.setPrefix("soapenv");

                    SOAPElement requestData = body.addChildElement("RequestData");
                    requestData.setPrefix("tem");

                    SOAPElement requestDoc = requestData.addChildElement("requestDocument","tem","http://tempuri.org/");

                    requestDoc.addTextNode(" <![CDATA[");

                    SOAPElement request = requestDoc.addChildElement("Request");

                    SOAPElement authentication = request.addChildElement("Authentication");
                    authentication.setAttribute("CMId", "68");
                    authentication.setAttribute("Guid", "5594FB83-F4D4-431F-B3C5-EA6D7A8BA795");
                    authentication.setAttribute("Password", "poihg321TR");
                    authentication.setAttribute("Function", "1");

                     SOAPElement establishment = request.addChildElement("Establishment");
                        establishment.setAttribute("Id", "4297867");
                     requestDoc.addTextNode("]]>");

                    System.out.println("\nSoap Request: \n\n\n"+getMsgAsString(sm));  

Output I am getting is when executing the code.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"><SOAP-ENV:Header/><soapenv:Body><tem:RequestData><tem:requestDocument xmlns:tem="http://tempuri.org/"> &lt;![CDATA[<Request><Authentication CMId="68" Function="1" Guid="5594FB83-F4D4-431F-B3C5-EA6D7A8BA795" Password="poihg321TR"/><Establishment Id="4297867"/></Request>]]&gt;</tem:requestDocument></tem:RequestData></soapenv:Body></soapenv:Envelope>  

The problem is

<![CDATA[     ]]>  is displaying like  &lt;![CDATA[ and ]]&gt;  

And my Server is not accepting this request.

Answer

helderdarocha picture helderdarocha · Jun 4, 2014

You need to create and add a CDATASection:

CDATASection cdata = 
    requestDoc.getOwnerDocument().createCDATASection("<element>text</element>");
requestDoc.appendChild(cdata);

This will produce:

<![CDATA[<element>text</element>]]>