How to unescape HTML character entities in Java?

前端 未结 11 1744
耶瑟儿~
耶瑟儿~ 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:32

    Incase you want to mimic what php function htmlspecialchars_decode does use php function get_html_translation_table() to dump the table and then use the java code like,

    static Map html_specialchars_table = new Hashtable();
    static {
            html_specialchars_table.put("<","<");
            html_specialchars_table.put(">",">");
            html_specialchars_table.put("&","&");
    }
    static String htmlspecialchars_decode_ENT_NOQUOTES(String s){
            Enumeration en = html_specialchars_table.keys();
            while(en.hasMoreElements()){
                    String key = en.nextElement();
                    String val = html_specialchars_table.get(key);
                    s = s.replaceAll(key, val);
            }
            return s;
    }
    

提交回复
热议问题