Mark Korzhov
Mark Korzhov

Reputation: 2159

Element id in the loop (JSOUP)

Here is my code:

   Element current = doc.select("tr[class=row]").get(5);   
   for (Element td : current.children()) {
          System.out.println(td.text());
   }

How can I get an Element id in the loop?

Thanks!

Upvotes: 1

Views: 402

Answers (1)

Andrey Chaschev
Andrey Chaschev

Reputation: 16526

In HTML id is a normal attribute, so you can simply call td.attr("id"):

Element current = doc.select("tr.row").get(5);
for (Element td : current.children()) {
    System.out.println(td.attr("id"));
}

Note that there is also a selector for classes: tr.row.

JSoup supports many of the CSS selectors, so this could be rewritten with a single selector:

Elements elements = doc.select("tr.row:nth-of-type(6) > td");

for (Element element : elements) {
    System.out.println(element.id());
}

Upvotes: 2

Related Questions