我正在尝试解析一个 html,例如:
<html>
<body>
<header>hello</header>
<p>
this is a text<br/>
</p>
</body>
</html>
我想提取
<p>
this is a text<br/>
</p>
我尝试过 Jsoup:
private String parseTicketReply(String content) {
Document doc = Jsoup.parse(content);
String replybody = "";
// zimbra
if (doc.select("body").size() > 0) {
replybody = doc.select("body").get(0).text();
}
return replybody;
}
我没有得到我想要的东西。我得到“这是一条文本”。
我尝试了 SAX perser、neko 等...它们都没有给我正确的结果。有人知道如何以正确的方式做到这一点吗?真的很挣扎。提前致谢...
最佳答案
改变
replybody = doc.select("body").get(0).text();
到
replybody = doc.select("body").get(0).html();
您可以使用doc.body()
代替doc.select("body")
在 http://jsoup.org/cookbook/introduction/parsing-a-document
The parser will make every attempt to create a clean parse from the HTML you provide, regardless of whether the HTML is well-formed or not.
关于java - 如何在标签完好无损的情况下解析 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18763122/