StringEscapeUtils.escapeXml says we should use
StringEscapeUtils.ESCAPE_XML.with( new UnicodeEscaper(Range.between(0x7f, Integer.MAX_VALUE)) );
But instead of UnicodeEscaper you need to use NumericEntityEscaper . UnicodeEscaper will change everything to \u1234 , but NumericEntityEscaper as { It was expected.
package mypackage; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.text.translate.CharSequenceTranslator; import org.apache.commons.lang3.text.translate.NumericEntityEscaper; public class XmlEscaper { public static void main(final String[] args) { final String xmlToEscape = "<hello>Hi</hello>" + "_ _" + "__ __" + "___ ___" + "after ";
source share