使用 Jericho,我需要解析如下内容:
<html>
<div class="title">
Spoon bows
<br/>
<span>
A Matrix scene.
<br/>
Matrix 1
</span>
</div>
</html>
我想解析“Spoon bows”,但我在 <div>
中得到了全部内容使用以下代码标记:
List<Element> list = item.getAllElementsByClass("title");
if(list!=null) {
Element title = list.get(0);
if(title!=null) {
String text = title.getContent().getTextExtractor().toString();
}
}
}
最佳答案
这对您有帮助:
private String getTextContent(Element elem) {
String text = elem.getContent().toString();
final List<Element> children = elem.getChildElements();
for (Element child : children) {
text = text.replace(child.toString(), "");
}
return text;
}
关于java - 如何使用 Jericho 解析没有嵌套 html 元素的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9795436/