如何使用java从SOAP响应中删除(<)和([CDATA [[)?

时间:2012-01-11 03:39:14

标签: java soap response

请帮我解决下面的问题,我很长时间(3周)但是无法获得正确的解决方案。我必须尽快完成它,因为我的这么多webservice输出取决于自定义返回字符串

我使用javax.jws编写了java代码。

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;

    String returnStr = null;

    String responseCodeStart = "<responseCode>";
    String responseCodeEnd = "</responseCode>";
    String responseMessageStart = "<responseMessage>";
    String responseMessageEnd = "</responseMessage>";

将JAVA中的自定义字符串返回给SOAP响应/输出字符串:::::::::

 returnStr = "\r\n" + responseCodeStart + "1000" + responseCodeEnd + "\r\n" + responseMessageStart + "Registration successful>>" + responseMessageEnd + "\r\n";

问题:

1)  I am getting (&lt;) tag instead of (<)
2) I am getting ([CDATA[[) tag in response.

SOAP响应:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:registerUserResponse xmlns:ns2="http://impl.timecapsule.com/">
         <return>&lt;responseCode>1102&lt;/responseCode>
&lt;responseMessage> [!CDATA[[Please provide username] &lt;/responseMessage></return>
      </ns2:registerUserResponse>
   </soap:Body>
</soap:Envelope>

* 需要解决方案: *

    1) &lt; to remove
    2) CDATA to remove

I want to retrieve it as XML object instead of String using java code.


THANKS

1 个答案:

答案 0 :(得分:2)

如果要通过SOAP提供或使用Web服务,请使用Framework。 您不必使用任何起始标签或结束标签,框架将处理此问题。

String responseCodeStart = "<responseCode>";
String responseCodeEnd = "</responseCode>";
String responseMessageStart = "<responseMessage>";
String responseMessageEnd = "</responseMessage>";

如果返回String(不是类中的数字),则编码是正确的。您返回带有特殊编码Chars的String。他们必须以这种方式编码。否则你将创建格式错误的XML。

javax.jws Tutorial at Oracle Webservice-Tutorial