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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: