43

I would like to convert some HTML characters back to text using Java Standard Library. I was wondering whether any library would achieve my purpose?

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

    // "Happy & Sad" in HTML form.
    String s = "Happy & Sad";
    System.out.println(s);

    try {
        // Change to "Happy & Sad". DOESN'T WORK!
        s = java.net.URLDecoder.decode(s, "UTF-8");
        System.out.println(s);
    } catch (UnsupportedEncodingException ex) {

    }
}
Zach Scrivena
  • 29,073
  • 11
  • 63
  • 73
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

8 Answers8

60

I think the Apache Commons Lang library's StringEscapeUtils.unescapeHtml3() and unescapeHtml4() methods are what you are looking for. See https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/StringEscapeUtils.html.

jsheeran
  • 2,912
  • 2
  • 17
  • 32
Bill.D
  • 166
  • 3
  • 5
  • 1
    Up to date url: http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html – Reu Nov 23 '11 at 16:57
  • 1
    Not to beat a dead horse, but what the OP was asking for was how to translate between HTML entities and "plain" text (which is ASCII for me, but YMMV). The Jakarta lib above has unescapeHTML (and escapeHTML), which does the trick. URLDecoder still works for percent-encoding URL strings (like GET parameters). – jjohn Jun 14 '12 at 18:20
  • How same will support in case of Android, any idea? – CoDe Sep 13 '13 at 19:49
  • Better to give the main url, the specific versions can be deleted ;) => http://commons.apache.org/proper/commons-lang/ – Seynorth Dec 17 '13 at 15:23
  • 1
    StringEscapeUtils is deprecated. The reply just below is now the most correct. – allemattio Aug 11 '17 at 12:46
  • No offense, I'm a fervent Apache commons supported and an Apache Fundation Software member but I agree wth allemattio. From experience I'd rather use jsoup as suggested below. – JacquesLeRoux Nov 27 '17 at 13:20
  • You please add the *gradle implementation* for the next beacuse i almost can fount that. implementation 'org.apache.commons:commons-text:1.0' – SonickSeven Jun 25 '20 at 02:53
28

Here you have to just add jar file in lib jsoup in your application and then use this code.

import org.jsoup.Jsoup;

public class Encoder {
    public static void main(String args[]) {
        String s = Jsoup.parse("<Français>").text();
        System.out.print(s);
    }
}

Link to download jsoup: http://jsoup.org/download

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
jem
  • 41
  • 2
  • 4
7

java.net.URLDecoder deals only with the application/x-www-form-urlencoded MIME format (e.g. "%20" represents space), not with HTML character entities. I don't think there's anything on the Java platform for that. You could write your own utility class to do the conversion, like this one.

Zach Scrivena
  • 29,073
  • 11
  • 63
  • 73
5

The URL decoder should only be used for decoding strings from the urls generated by html forms which are in the "application/x-www-form-urlencoded" mime type. This does not support html characters.

After a search I found a Translate class within the HTML Parser library.

Rich
  • 325
  • 2
  • 6
4

You can use the class org.apache.commons.lang.StringEscapeUtils:

String s = StringEscapeUtils.unescapeHtml("Happy & Sad")

It is working.

pirho
  • 11,565
  • 12
  • 43
  • 70
2

Or you can use unescapeHtml4:

    String miCadena="GUÍA TELEFÓNICA";
    System.out.println(StringEscapeUtils.unescapeHtml4(miCadena));

This code print the line: GUÍA TELEFÓNICA

2

I'm not aware of any way to do it using the standard library. But I do know and use this class that deals with html entities.

"HTMLEntities is an Open Source Java class that contains a collection of static methods (htmlentities, unhtmlentities, ...) to convert special and extended characters into HTML entitities and vice versa."

http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=htmlentities

rogeriopvl
  • 51,659
  • 8
  • 55
  • 58
1

As @jem suggested, it is possible to use jsoup.

With jSoup 1.8.3 it il possible to use the method Parser.unescapeEntities that retain the original html.

import org.jsoup.parser.Parser;
...
String html = Parser.unescapeEntities(original_html, false);

It seems that in some previous release this method is not present.

Evan Knowles
  • 7,426
  • 2
  • 37
  • 71
Daniele
  • 821
  • 7
  • 18