Dragan Menoski
Dragan Menoski

Reputation: 1102

How to select all children (with same tag. ex.table) except first and last with jsoup

I want to get all tags (with same tag. ex. table) in one div with id = content, except first and last. The number of tags (in this case tables) is dynamic.

enter image description here

Upvotes: 2

Views: 1062

Answers (2)

siledh
siledh

Reputation: 3378

You can get all of them (I assume you know how to do that, otherwise the question would be stated differently?), write to a list, let's call it tables, and then do tables.sublist(1, tables.size() - 1)

Here is the full solution using selectors

Document doc = Jsoup.parse(...) // parse from some source
Elements tables =  doc.select("div#content table");
tables = tables.sublist(1, tables.size() - 1);

Upvotes: 3

Admit
Admit

Reputation: 4987

Excerpt from doc about selectors:

el, el, el: group multiple selectors, find unique elements that match any of the selectors; e.g. div.masthead, div.logo

:not(selector): find elements that do not match the selector

:last-child elements that are the last child of some other element.

:gt(n): find elements whose sibling index is greater than n; e.g. div p:gt(2)

I guess it's a good starting point.

More here

Upvotes: -1

Related Questions