我只是想在我的HTML中转义所有PRE标签的内容。要做到这一点,我目前正在使用JSoup,除了一件事,它正在按预期工作。我正在使用的示例输入字符串:
<pre>public List<Article> methodName() {
...
}</pre>
因此,从这个字符串中,我只想逃避<
和>
个字符。我目前正在使用JSoup执行此操作,如下所示(我使用Spring HtmlUtils进行转义):
Document document = Jsoup.parse(string);
document.outputSettings().prettyPrint(false);
Elements codeTags = document.select("pre");
for (Element codeTag : codeTags) {
codeTag.html(HtmlUtils.htmlEscape(codeTag.html()));
}
除了上面的输入字符串之外,它似乎按预期工作,它似乎会自动更改并“修复”<Article>
文本,如下所示:
<pre>public List<article> methodName() {
...
}</article></pre>
我知道JSoup正在解析HTML,但这不是我想要的行为,在这种情况下,有什么我可以做的告诉JSoup不要尝试自动修复我的HTML吗?我应该首先使用JSoup吗?