0

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

jeph perro
  • 6,242
  • 26
  • 90
  • 124
  • possible duplicate of [Best way to encode text data for XML in Java?](http://stackoverflow.com/questions/439298/best-way-to-encode-text-data-for-xml-in-java) – Ted Hopp Sep 13 '11 at 00:24

2 Answers2

1

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.

Community
  • 1
  • 1
scartag
  • 17,548
  • 3
  • 48
  • 52
0

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

Community
  • 1
  • 1
Jon7
  • 7,165
  • 2
  • 33
  • 39