0

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 '+'?

edoreld
  • 303
  • 1
  • 17
  • If you encoded with URLEncoder, wouldn’t it make sense to decode using [URLDecoder](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/net/URLDecoder.html)? – VGR Dec 17 '19 at 15:14

1 Answers1

0

Hi I think the following link would be helpful URLEncoder not able to translate space character

One question , why do you need URL based encoding feature to encode your xml ?

prashant s
  • 148
  • 1
  • 1
  • 9
  • Why do I need URL based encoding? I don't know that I need to, but I am not aware of an alternative. Am I supposed to use an XML encoding library? – edoreld Dec 17 '19 at 13:14
  • @edoreld the URL encoder is basically to construct URLs with certain symbols in its original form will not be supported. if you are passing the xml as a request parameter then this would suit you. – prashant s Dec 17 '19 at 13:27