1

使用 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();
        }
    }
}
4

2 回答 2

6

这应该可以帮助您:

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;
}
于 2012-11-13T02:44:05.600 回答
1

也许您可以遍历标题节点的子元素。

看看这个问题:How to iterate over plain text segments with the Jericho HTML parser

于 2012-10-25T06:31:07.223 回答