jeph perro
jeph perro

Reputation: 6422

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

Upvotes: 0

Views: 1649

Answers (2)

Jon7
Jon7

Reputation: 7225

This doesn't match your question exactly, but it appears that the answer is what you're looking for Recommended method for escaping HTML in Java.

Summary: StringEscapeUtils.escapeHtml() from apache commons

Upvotes: 0

scartag
scartag

Reputation: 17680

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.

Upvotes: 1

Related Questions