How do you XML encode your data?

I was using jqgrid to display some "filtered" data on grid, but some filters won't display any data. My immediate suspect was special characters that may interfere with XML markup.

jqgrid table

After looking at Extensible Markup Language (XML) 1.0 (Second Edition) specification, disallowed characters are defined in IETF RFC 2396 except for (#), (%) and square bracket characters are re-allowed. Per W3 recommendation, there are three ways as recommended.

  • Each disallowed character is converted to UTF-8
  • Any octets corresponding to a disallowed character are escaped with the URI escaping mechanism (that is, converted to %HH, where HH is the hexadecimal notation of the byte value).
  • Better yet, enclose your data in <![CDATA[ your text here ]]>
Tags: 

Comments

We've noticed that IE11 does not observe <![CDATA[ your text here ]]>.

To escape XML, use StringEscapeUtils.

By Scott Seong (not verified)

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.