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 !
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 !
Comments
Muhammad Ammad
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.