ef2011
ef2011

Reputation: 10631

Jsoup: "Which span does this element belong to?"

Assuming I have found an element using:

Element link = div.select("a:not([class])").first();

I now want to find out whether this particular element is enclosed within a <span class="uniqspan"> -- not necessarily a direct descendant.

Is there a way to do that in Jsoup?

Update: I just found the wonderful Element.parent(). I am going to check whether this can be a start...

Upvotes: 2

Views: 274

Answers (1)

BalusC
BalusC

Reputation: 1109132

That's not directly possible from the Element link on. I'd suggest to collect all links in a span.uniqspan first and then check if it contains the link.

E.g.

Element link = div.select("a:not([class])").first();
Elements linksInUniqspan = document.select("span.uniqspan a:not([class])");

if (linksInUniqspan.contains(link)) {
    // Link is inside span.uniqspan.
} else {
    // Link is not inside span.uniqspan.
}

Upvotes: 2

Related Questions