Java one-liner: XML text escapes

It never occured to me before today but

str.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;");

is good enough for quick and dirty escaping of XML text.

And escaping it manually to write it here is also fun ;-)

4 Responses to Java one-liner: XML text escapes

  1. IIRC, the “>” is not really needed. The corresponding python idiom is:

    >>> str='<em>AT&T</em>’
    >>> ‘&lt;’.join(‘&amp;’.join(str.split(‘&’)).split(‘<‘))
    &lt;em>AT&amp;T&lt;/em>’

  2. Yonik Seeley says:

    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).

  3. panzi says:

    from xml.sax.saxutils import quoteattr, escape

%d bloggers like this: