Question

Using Jericho, I need to parse something like this:

<html>
<div class="title">
    Spoon bows
    <br/>
    <span>
        A Matrix scene.
        <br/>
        Matrix 1
    </span>
</div>
</html>

I want to parse "Spoon bows", but I get the whole content within the <div> tag using the following code:

List<Element> list = item.getAllElementsByClass("title");
if(list!=null) {
    Element title = list.get(0);
    if(title!=null) {
        String text = title.getContent().getTextExtractor().toString();
        }
    }
}
Was it helpful?

Solution

This should help you:

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;
}

OTHER TIPS

Maybe you could iterate over children elements of your title node.

Take a look at this question: How to iterate over plain text segments with the Jericho HTML parser

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top