[英]how to unescape html entities **except** < > & " ' in java
I have html input in utf-8. 我在utf-8中输入了html。 In this input accented characters are presented as html entities.
在此输入中,带重音的字符表示为html实体。 For example:
例如:
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>árvíztűrő<b</body>
</html>
My goal is to "canonicalize" the html by replacing html entities with utf-8 characters where possible in Java. 我的目标是通过在Java中尽可能用utf-8字符替换html实体来“规范化” html。 In other words, replace all entities except
< > & " '
换句话说,替换除
< > & " '
之外的所有实体< > & " '
< > & " '
. 。
The goal: 目标:
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>árvíztűrő<b</body>
</html>
I need this to make it easier to compare htmls in tests, and to be easier to read for the naked eye (lots of escaped accented characters makes it very hard to read). 我需要这样做,以使其更容易在测试中比较html,并更易于肉眼阅读(许多逃脱重音字符使其很难阅读)。
I don't care cdata sections (there's no cdata in the inputs). 我不在乎cdata节(输入中没有cdata)。
I have tried JSOUP ( https://jsoup.org/ ) and Apache's Commons Text ( https://commons.apache.org/proper/commons-text/ ) unsuccessfully: 我尝试了JSOUP( https://jsoup.org/ )和Apache的Commons Text( https://commons.apache.org/proper/commons-text/ ),但未成功:
public void test() throws Exception {
String html =
"<html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">" +
"</head><body>árvíztűrő<b</body></html>";
// this is not good, keeps only the text content
String s1 = Jsoup.parse(html).text();
System.out.println("s1: " + s1);
// this is better, but it unescapes the < which is not what I want
String s2 = StringEscapeUtils.unescapeHtml4(html);
System.out.println("s2: " + s2);
}
The StringEscapeUtils.unescapeHtml4() is almost what I need, but it unfortunately unescapes the < also: StringEscapeUtils.unescapeHtml4()几乎是我所需要的,但不幸的是,它还取消了<并且:
<body>árvíztűrő<b</body>
How should I do it? 我该怎么办?
Here is a minimal demonstration: https://github.com/riskop/html_utf8_canon.git 这是一个最小的演示: https : //github.com/riskop/html_utf8_canon.git
Looking into the Commons Text source it is clear that StringEscapeUtils.unescapeHtml4() delegates work to an AggregateTranslator, which is composed of 4 CharSequenceTranslator: 查看Commons Text源代码,很明显StringEscapeUtils.unescapeHtml4()委托工作由一个由4个CharSequenceTranslator组成的AggregateTranslator:
new AggregateTranslator(
new LookupTranslator(EntityArrays.BASIC_UNESCAPE),
new LookupTranslator(EntityArrays.ISO8859_1_UNESCAPE),
new LookupTranslator(EntityArrays.HTML40_EXTENDED_UNESCAPE),
new NumericEntityUnescaper()
);
I need only three of the translators to fullfill my goal. 我只需要三名翻译即可完成我的目标。
So this is it: 就是这样:
// this is what I needed!
String s3 = new AggregateTranslator(
new LookupTranslator(EntityArrays.ISO8859_1_UNESCAPE),
new LookupTranslator(EntityArrays.HTML40_EXTENDED_UNESCAPE),
new NumericEntityUnescaper()
).translate(html);
System.out.println("s3: " + s3);
Whole method: 整个方法:
@Test
public void test() throws Exception {
String html =
"<html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">" +
"</head><body>árvíztűrő<b</body></html>";
// this is what I needed!
CharSequenceTranslator UNESCAPE_HTML_EXCEPT_BASIC = new AggregateTranslator(
new LookupTranslator(EntityArrays.ISO8859_1_UNESCAPE),
new LookupTranslator(EntityArrays.HTML40_EXTENDED_UNESCAPE),
new NumericEntityUnescaper()
);
String s3 = UNESCAPE_HTML_EXCEPT_BASIC.translate(html);
System.out.println("s3: " + s3);
}
Result: 结果:
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>árvíztűrő<b</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.