Please note this is not the same question as mentioned above since XML escaping to preserve codepoints is possible.
I have a UTF-8 XML file which I can send via HTTP to some other system which I have no control over. For whatever crazy reason it decides to convert it to ISO-8859-1 loosing many Unicode characters and replacing them with '?'. This system then sends someone else this converted XML document.
How in Java on the sending side can I escape any arbitrary XML with non ASCII codepoints so that they survive this intermediary system and can still be decoded correctly by the endpoint?
A --(UTF-8)--> B --(ISO-8859-1)--> C (Decodes to internal Unicode representation).
import java.text.Normalizer;
import java.text.Normalizer.Form;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.text.translate.CharSequenceTranslator;
import org.apache.commons.lang3.text.translate.NumericEntityEscaper;
public class Test {
private static CharSequenceTranslator translator = StringEscapeUtils.ESCAPE_XML
.with(NumericEntityEscaper.between(0x7f, Integer.MAX_VALUE));
public static void main(String[] args) {
String s = "<note>\n<to>Tove</to>\n<from>Jani</from>\n<heading>Reminder</heading>\n<body>Don't forget me this weekend!test☠ä</body>\n</note>";
String xmlEscapedS = xmlToRobustXml(s);
System.out.println(xmlEscapedS);
}
/**
* @param s
* @return
*/
public static String xmlToRobustXml(String s) {
s = Normalizer.normalize(s, Form.NFC);
String xmlEscapedS = translator.translate(s);
return xmlEscapedS;
}
}
I tried this but it escapes everything.
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!test☠ä</body>
</note>