How to unescape HTML character entities in Java?

前端 未结 11 1894
耶瑟儿~
耶瑟儿~ 2020-11-21 22:38

Basically I would like to decode a given Html document, and replace all special chars, such as \" \" -> \" \", \">\" -

11条回答
  •  余生分开走
    2020-11-21 23:25

    A very simple but inefficient solution without any external library is:

    public static String unescapeHtml3( String str ) {
        try {
            HTMLDocument doc = new HTMLDocument();
            new HTMLEditorKit().read( new StringReader( "" + str ), doc, 0 );
            return doc.getText( 1, doc.getLength() );
        } catch( Exception ex ) {
            return str;
        }
    }
    

    This should be use only if you have only small count of string to decode.

提交回复
热议问题