Best way to encode text data for XML in Java?

Epaga picture Epaga · Jan 13, 2009 · Viewed 184.1k times · Source

Very similar to this question, except for Java.

What is the recommended way of encoding strings for an XML output in Java. The strings might contain characters like "&", "<", etc.

Answer

Fabian Steeg picture Fabian Steeg · Jan 13, 2009

As others have mentioned, using an XML library is the easiest way. If you do want to escape yourself, you could look into StringEscapeUtils from the Apache Commons Lang library.