Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create SOAP request with CDATA

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.

like image 664
LMK Avatar asked Jun 04 '14 04:06

LMK


People also ask

What is CDATA in SOAP request?

The term CDATA is used about text data that should not be parsed by the XML parser. Some registries mostly Immunization registries need the HL7 message that is in the soap envelope to be wrapped with this tag. Add "<![ CDATA[" before the hl7 message. Add "]]>" to after the hl7 message.

How do I make a SOAP request?

To make SOAP requests to the SOAP API endpoint, use the "Content-Type: application/soap+xml" request header, which tells the server that the request body contains a SOAP envelope. The server informs the client that it has returned a SOAP envelope with a "Content-Type: application/soap+xml" response header.

How do I add CDATA to XML?

CDATA sections can appear inside element content and allow < and & character literals to appear. A CDATA section begins with the character sequence <! [CDATA[ and ends with the character sequence ]]>. Between the two character sequences, an XML processor ignores all markup characters such as <, >, and &.


1 Answers

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>]]>
like image 114
helderdarocha Avatar answered Sep 29 '22 16:09

helderdarocha