Search code examples
javahtmljsoup

Use jsoup to encode Html characters


I have to encode characters to Html:

< to &lt;
> to &gt;
' to &#39;
" to &quot;
& to &amp;

I look for a utility function like htmlspecialchars in PHP:

String htmlspecialchars(String inputText)

Is it possible to use JSoup to encode these characters?

(I have found htmlEscape in the Spring framework, but I don't want to use the Spring framework just for this simple function.)


Solution

  • JSoup is a library to parse the HTML.

    I dont think you can use it to encode special characters into HTML.

    The best way to do it, is to write your own method. Simply you can grab this method from Spring and you dont need to setup whole framework. See source code.