I have the following valid XML document:
<bundle><elem type="A"></elem></bundle>
I want to encode this XML string so as to send it to a third-party web service. This service requires XML to be sent encoded as a query parameter. I am trying to encode my XML using URLEncoder as follows:
rawXml = "<bundle><elem type=\"A\"></elem></bundle>";
encodedXML = URLEncoder.encode(rawXML, "UTF-8");
This generates the following encoded string:
%3Cbundle%3E%3Celem+type%3D%22A%22%3E%3C%2Felem%3E%3C%2Fbundle%3E
When I decode this, for example using this decoder, I get the following decoded XML document:
<bundle><elem+type="A"></elem></bundle>
Which is indeed a valid document. However, when I send it to the third-party web service as a query parameter, the service rejects it, even when I set the Content-Type header to application/x-www-form-urlencoded, which is the format used by URLEncoder.
The problem lies in the plus sign. If I manually change this plus sign to "%20", the service accepts my request.
My question is, is there a way to encode an XML string so that spaces get mapped to "%20" instead of '+'?