我不知道“保留原始文本的索引”部分,但Jericho是一个非常好的 HTML 解析器库。
以下是如何从 html 中删除每个跨度的示例:
public static String removeSpans(String html) {
Source source = new Source(html);
source.fullSequentialParse();
OutputDocument outputDocument = new OutputDocument(source);
List<Tag> tags = source.getAllTags();
for (Tag tag : tags) {
String tagname = tag.getName().toLowerCase();
if (tagname.equals("span")) {
//remove the <span>
outputDocument.remove(tag);
}
}
return outputDocument.toString();
}
Run Code Online (Sandbox Code Playgroud)