Search code examples
javastringxhtmlencodehtml-encode

How to I encode a string in java to vaild xHTML


I am creating a web service in Java and I want to clean up my strings so they are valid xHTML. I'd also like to preserve line breaks.

So for example, if my user enters a string like this:

 This course is offered <3> times a year & is scheduled for: 
 October 2011
 November 2012 and
 December  2013

I want to clean up the string to look like this:

 This course is offered &lt;3&gt; times a year &amp is scheduled for: 
 October 2011
 November 2012 and
 December  2013

How can I achieve this in Java?

Thanks


Solution

  • This question has been asked before here , but it requires taking a dependency on an external library since the JDK doesn't support it directly. check it out. The top answer on the page.