I am using JAX-WS with the JAX-WS reference implementation. I'm creating a Web Service that gets XML to store and later can return that previously stored XML.
@WebMethod(operationName="store")
public void store(@WebParam(name="XML") String XML)
{
...
}
@WebMethod(operationName="retrieve")
public String retrieve()
{
String s = XML;
return s;
}
My Interface uses the type String for the XML. When I send the XML data, I wrap the whole string into a CDATA-Tag (since I need to send characters like < and > within the soap message). However when the Web Service later returns that XML data, strange things happen:
If the string is long enough, the WS wraps everything into CDATA, that's the way I want it.
However if the string is really short (e.g. small XML instances), the single characters are escaped (e.g. < becomes <). This is NOT what I want because it makes it really hard to work with the data. Besides, if my string contains characters like &
because I stored escaped text, this gets killed and becomes &
How can I force my Web Service to return the string always within CDATA and not escape anything by itself? Simply appending <![CDATA[
and ]]>
before and after the string right before returning the value does not help because the CDATA-Markup itself then gets escaped.
BTW: I think the fact that I store and retrieve XML does not even matter. The same would be the problem if I store usual strings containing characters like '<', '>' and '&' (which is essentially what I do). If I store them via CDATA but do not get them back via CDATA but with every single special characters escaped independently, I have to reconstruct the whole string by myself. How can I enforce the use of CDATA when returning strings?