Java servlet json object containing XML, encoding problems

831 Views Asked by At

I have a servlet which should reply to requests in Json {obj:XML} (meaning a Json containing an xml object inside).

The XML is encoded in UTF-8 and has several chars like => पोलैंड.

The XML is in a org.w3c.dom.Document and I am using JSON.org library to parse JSON. When i try to print it on the ServletOutputStream, the characters are not well encoded. I have tested it trying to print the response in a file, but the encoding is not UTF-8.

Parser.printTheDom(documentFromInputStream,byteArrayOutputStream);
OutputStreamWriter oS=new OutputStreamWriter(servletOutputStream, "UTF-8");
oS.write((jsonCallBack+"("));
oS.write(byteArrayOutputStream);
oS.write(");");

I have tryed even in local (without deploing the servlet) the previous and the next code :

oS.write("पोलैंड");

and the result is the same.

Instead when I try to print the document,the file is a well formed xml.

 oS.write((jsonCallBack+"("));
 Parser.printTheDom(documentFromInputStream,oS);
 oS.write(");");

Any help?

1

There are 1 best solutions below

1
Asaph On

Typically, if binary data needs to be part of an xml doc, it's base64 encoded. See this question for more details. I suggest you base64 encode the fields that can have exotic UTF-8 chars and and base64 decode them on the client side.

See this question for 2 good options for base64 encoding/decoding in java.