如何使用CDATA创建SOAP请求



我是SOAP新手,我想创建SOAP请求,如下所示

<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>  
我在教程 中找到了创建SOAP请求的代码
  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: nnn"+getMsgAsString(sm));  

我得到的输出是在执行代码时。

<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>  

问题是

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

我的服务器不接受这个请求

您需要创建并添加一个CDATASection:

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

这将产生:

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

最新更新