1

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
pokeRex110
  • 833
  • 2
  • 14
  • 26
  • Have you tried using the `Writer` supplied by `HttpServletResponse.getWriter()`, rather than manually wrapping the raw output stream? – skaffman Jan 12 '12 at 16:45
  • Hi, thank you vm for your comment. I tried to use the writer, and it appear to work just in some cases, other chars are not well formed. Do you have any additional idea? @skaffman – pokeRex110 Jan 16 '12 at 10:11

1 Answers1

0

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.

Community
  • 1
  • 1
Asaph
  • 159,146
  • 25
  • 197
  • 199
  • Thank you vm, I will try it out. Anyhow, why does this works on php? In php the same code (more or less) produces the hex represtation of the exotic chars. Thanks again for the answer. – pokeRex110 Jan 13 '12 at 08:22