我是 jsoup 的新手,在使用非 HTML 元素(脚本)时遇到了一些困难。我有以下 HTML:
<$if not dcSnippet$>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="generator" content="Outside In HTML Converter version 8.4.0"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<$endif$>
<div style="position:relative">
<p style="text-align: left; font-family: times; font-size: 10pt; font-weight: normal; font-style: normal; text-decoration: none"><span style="font-weight: normal; font-style: normal">This is a test document.</span></p>
</div>
<$if not dcSnippet$>
</body>
</html>
<$endif$>
用于显示此内容的应用程序知道如何处理这些 <if dcSnippet$> 等语句。所以,当我简单地用 jsoup 解析文本时,< 和 > 被编码并且 html 被重新组织,所以它不能正确执行或显示。像这样:
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><$if not dcSnippet$>
<meta http-equiv="generator" content="Outside In HTML Converter version 8.4.0">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<$endif$>
<div style="position:relative">
<p style="text-align: left; font-family: times; font-size: 10pt; font-weight: normal; font-style: normal; text-decoration: none"><span style="font-weight: normal; font-style: normal">This is a test document.</span></p>
</div>
<$if not dcSnippet$>
<$endif$>
</body></html>
我的最终目标是添加一些 css 和 js 包含,并修改几个元素属性。这不是问题,我已经解决了很多问题。问题是我不知道如何保留非 HTML 元素并将格式保存在与原始元素相同的位置。到目前为止,我的解决方案是这样的:
- 读入 HTML 文件,并遍历它,删除包含非 html 元素的行。
- 使用纯 HTML 创建一个 Document 对象
- 进行我的修改
- 返回 HTML 并重新插入我首先删除的非 HTML 元素(脚本)。
- 将文档保存到文件系统
这目前有效,只要非 HTML 的位置是可预测的,到目前为止它是可预测的。但我想知道是否有更好的方法来做到这一点,所以我不必先“清理”HTML,然后手动重新引入我稍后删除的内容。这是我的代码的要点(希望我没有错过太多声明):
String newLine();
FileReader fr = new FileReader(inputFile);
BufferedReader br = new BufferedReader(fr);
while ((thisLine = br.readLine()) != null) {
if (thisLine.matches(".*<\\$if.*\\$>")) {
ifStatement = thisLine + "\n";
} else if (thisLine.matches(".*<\\$endif\\$>")) {
endifStatement = thisLine + "\n";
} else {
tempHtml += thisLine + "\n";
}
}
br.close();
Document doc = Jsoup.parse(tempHtml, "UTF-8");
doc.outputSettings().prettyPrint(false).escapeMode(EscapeMode.extended);
Element head = doc.head();
Element body = doc.body();
Element firstDiv = body.select("div").first();
[... perform my element and attribute inserts ...]
body.prependText("\n" + endifStatement);
body.appendText("\n" + ifStatement);
String fullHtml = (ifStatement + doc.toString().replaceAll("\\<", "<").replaceAll("\\>", ">") + "\n" + endifStatement);
BufferedWriter htmlWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
htmlWriter.write(fullHtml);
htmlWriter.flush();
htmlWriter.close();
非常感谢您的任何帮助或意见!