Reputation: 11
This how the DOM looks:
<div class="content-section generic-section">
<div class="content-section generic-section">
<div class="content-section generic-section">
I need to fetch the contents from each div elements. While using select query all three contents are fetched in same variable. how to solve this issue.
Upvotes: 1
Views: 1368
Reputation: 47895
Re this:
While using select query all three contents are fetched in same variable.
All three would be assigned to an instance of Elements
which is a type of ArrayList
so, in order to interrogate each of these three elements you could
for (Element element : elements) { ... }
elements.get(0)
, elements.get(1)
etcElements
using prev()
and next()
first()
or last()
traverse()
the Elements
text
of each of the entries: elements.eachText()
Even more options in the Javadocs
Upvotes: 2
Reputation: 181
You can get an Elements object using select method. As @glyching mentioned, you can traverse it. I created test code using forEach like as below.
public void test() {
Document doc = Jsoup.parse("<html><body><div class=\"content-section generic-section\">contents1</div><div class=\"content-section generic-section\">contents2</div><div class=\"content-section generic-section\">contents3</div></body></html>");
// get div elements
Elements elements = doc.select("div.content-section.generic-section");
// display "contents1" "contents2" "contents3"
elements.forEach(element -> System.out.println(element.text()));
}
Upvotes: 0