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) {

    }
}
share|improve this question
up vote 53 down vote accepted

I think the Jakarta Commons Lang library's StringEscapeUtils.escapeHtml() and unescapeHtml() methods are what you are looking for. See http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/StringEscapeUtils.html .

share|improve this answer
1  
Up to date url: commons.apache.org/lang/api-2.6/org/apache/commons/lang/… – Reu Nov 23 '11 at 16:57
    
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 ;) => 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

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

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

It is working.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer
    
This should be the accepted answer. No other library is faster nor easier to import than the amazing Jsoup. – Grux May 13 '15 at 8:55
    
Awesome. This is answer. – Sattar Hummatli Aug 17 '16 at 12:24

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.

share|improve this answer
    
very good library, now it's easy to do something like <github.com/jgm/pandoc/blob/master/src/Text/Pandoc/Writers/…‌​; – Miguel Aug 17 '12 at 14:58

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.