superuser
superuser

Reputation: 731

Android parse html table with JSoup

I have looked around on stack overflow and I have found the following posts:

https://stackoverflow.com/questions/7864433/how-to-parse-the-cells-of-the-3rd-column-of-a-tablehttps://stackoverflow.com/questions/7864433/how-to-parse-the-cells-of-the-3rd-column-of-a-table

but I am still a bit confused.

What I have is a html table that contains a few of these inside the <tbody> tag with different dates:

            <tr>
                <td>
                    <nobr>Fri</nobr>
                </td>
                <td>
                    <nobr>Sep 20</nobr>
                </td>
                <td>
                    <nobr>4:00 PM</nobr>
                </td>
                <td>
                    Practice                </td>
            </tr>

The table's id is "gymschedule".

So far I can get the table with JSoup and I can display it in a webview with JSoup.

What I need is to get the text of the second <nobr> in the second <td>, and also do this for every other group of <tr> tags in the table.

Upvotes: 1

Views: 1929

Answers (1)

Daniel B
Daniel B

Reputation: 8879

I do not know what the original source looks like completely, but this should work.

You can use CSS-selectors to select specific tags in your document, and specify what properties they should have, using pseudo selectors.

If you would want to select only the <tr>-tags that are the first in a sequence of many, you would use the tr:eq(0) selector.

In your case you would end up with something like:

    doc = Jsoup.parse(html, "", Parser.xmlParser());
    Elements elements = doc.select("tr td:eq(1) nobr");
    for (Element e : elements) {
        System.out.println(e.text()); 
    }

which will print out

Sep 20

As I do not know how your complete source look like, you might be able to use the default HTML parser Jsoup.parse(html);, though that wont work on the snippet you have provided.

Examples of other pseudo selectors could be

:lt(0) //Less than
:gt(0) //Greater than

I suggest you read up on using selector-syntax.

Use selector-syntax to find elements

Upvotes: 2

Related Questions