It never occured to me before today but
str.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">");
is good enough for quick and dirty escaping of XML text.
And escaping it manually to write it here is also fun ;-)
It never occured to me before today but
str.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">");
is good enough for quick and dirty escaping of XML text.
And escaping it manually to write it here is also fun ;-)
IIRC, the “>” is not really needed. The corresponding python idiom is:
>>> str='<em>AT&T</em>’
>>> ‘<’.join(‘&’.join(str.split(‘&’)).split(‘<‘))
<em>AT&T</em>’
If Lang is in your classpath:
http://jakarta.apache.org/commons/lang/api-release/org/apache/commons/lang/StringEscapeUtils.html#escapeXml(java.lang.String)
Since ]]> is not legal, it’s often easiest to just escape all ‘>’.
And unfortunately, while this is good enough to escape all “text”, it’s not good enough to escape all Java Strings, or all of unicode (some of the lower control chars are not valid XML, even if replaced by a numeric entity).
from xml.sax.saxutils import quoteattr, escape