这是一个建议:
public static Node toTextElement(String str) {
Element e = new Element(Tag.valueOf("text"), "");
e.appendText(str);
return e;
}
public static void replaceTextNodes(Node root) {
if (root instanceof TextNode)
root.replaceWith(toTextElement(((TextNode) root).text()));
else
for (Node child : root.childNodes())
replaceTextNodes(child);
}
测试代码:
String html = "
\"You began the evening well, Charlotte,\" " +
"said Mrs. Bennet with civil self–command to Miss Lucas." +
" \"You were Mr. Bingley's first choice.\"
";Document doc = Jsoup.parse(html);
for (Node n : doc.body().children())
replaceTextNodes(n);
System.out.println(doc);
输出:强>
"
You
began the evening well, Charlotte," said Mrs. Bennet with civil self–command to Miss Lucas. "
You
were Mr. Bingley's first choice."