Saturday, May 17, 2008

Escape HTML and XML in Java

HTML and XML escape format is widely used in web and XML transactions.

There is a great utility for this purpose in Java world : Lang package in apache commons project

Download commons-lang and put the commons-lang-xxx.jar file in your classpath or in your project.

The StringEscapeUtils class is your key class here.

Use static escapeXml() , escapeHtml() , unescapeXml() , unescapeHtml() methods.

String escaped = StringEscapeUtils.escapeXml("");

Great & Lifesaving , thanks apache again !


Járai Csaba said...


Anonymous said...


Anonymous said...


Emre said...

Thanks a lot :)

Anonymous said...

finally, someone who just tells you how to solve a problem! Thank you so much!

Anonymous said...

Thank you. It is great help
Muhammad Ammad

Anonymous said...

StringEscapeUtils.escapeXml() is fine if your text doesn't already include escaped characters.

However, if you try to escape the following string "This & That" you end up with "This & That".

In other words, it only performs a very simplistic search and replace of the ampersand character.

Anonymous said...

Note that the browser translated the amp; into the ampersand character!