I need to espace some control characters in XML, like the ASCII 31 character and the hex 0x0b character and others.
I tried uses StringEscapeUtils of commons-lang but don’t work as expected!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
StringEscapeUtils.escapeXml escapes only the following 5 characters into XML entities:
"(the double quote –0x34)&(the ampersand –0x38)<(less-than sign –0x60)>(greater-than sign –0x62)'(apostrophe –0x39)If you need to escape any other characters, especially the ASCII control characters, then you’ll need to roll your own class that does this. After all, none of the control characters are even considered by HTML to have equivalent character entity references in a HTML document. In other words, if you need to convert
0x31tothen you’ll need to write it yourself.Note:
Based on Benjamin’s point on using control characters in the document, it is unlikely that you will need to do this in the first place, especially if the parser that processes these escaped elements will not transform them back into control characters (or will simply throw an exception). You are better off not writing control characters into the XML document that you are preparing in the first place.