Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Selector for a TD in a Table with an ID

I have an html table with an ID of thetable. It has (according to FireBug), an unnamed TBODY tag, and an unnamed TR tag. The three TD tags inside that I want to access are unnamed. Is there any CSS selector that I can use to reference these unnamed tr tags?

I've tried:

table#thetable:nth-child(1)

But this does not select for those tags.

I'm using jsoup.org to pull this data into strings. I'd like to take the text of each TD in the table (of which I know the name) and put all that into an array.

Something like this:

// Pseudocode for all the TDs into an array
Elements strings = doc.select("table#thetable: children");
like image 314
Mark Lyons Avatar asked Apr 03 '12 03:04

Mark Lyons


2 Answers

If you're simply looking to get all your td elements, this should be enough:

Elements elems = doc.select("table#thetable td");

Then iterate elems, retrieve the text from your tds and put them in your array.

Also, you should probably use an array list instead of an array if you don't know or can't control how many cells your table will have:

Elements elems = doc.select("table#thetable td");
List<String> strings = new ArrayList<String>();

for (Element e : elems) {
    strings.add(e.text());
}
like image 191
BoltClock Avatar answered Oct 06 '22 00:10

BoltClock


You just want to access all of the cells?

#thetable td

Or do you just want to access the first row?

#thetable tr:first-of-type td
like image 44
Ry- Avatar answered Oct 05 '22 23:10

Ry-