Question

<table class="local_table nbu_rate">
    <tr>
        <th>Currency</th>
    <th class="align_left">Name</th>
        <th class="width_25">Course</th>
    </tr>
    <tr>
        <td>USD</td>
        <td class="align_left">USD</td>
        <td>11.2416</td>
    </tr>
    <tr>
        <td>EUR</td>
        <td class="align_left">Euro</td>
        <td>15.5078</td>
    </tr>
    </table>

How I can parse value from table. For example 11.24? I tried do this:

        try {
            doc = Jsoup.parse(url, 3000);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Element table = doc.select("table[class=local_table nbu_rate]").first();

        Iterator<Element> ite = table.select("td[USD]").iterator();

        System.out.println("Value 1: " + ite.next().text());

But my program is crashed. Log:

04-02 21:38:29.764: E/AndroidRuntime(5769): FATAL EXCEPTION: AsyncTask #1
04-02 21:38:29.764: E/AndroidRuntime(5769): java.lang.RuntimeException: An error occured while executing doInBackground()
04-02 21:38:29.764: E/AndroidRuntime(5769):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at java.lang.Thread.run(Thread.java:856)
04-02 21:38:29.764: E/AndroidRuntime(5769): Caused by: java.util.NoSuchElementException
04-02 21:38:29.764: E/AndroidRuntime(5769):     at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:572)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at com.example.parsedatatest.MainActivity$NewThread.doInBackground(MainActivity.java:92)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at com.example.parsedatatest.MainActivity$NewThread.doInBackground(MainActivity.java:1)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-02 21:38:29.764: E/AndroidRuntime(5769):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-02 21:38:29.764: E/AndroidRuntime(5769):     ... 4 more
Was it helpful?

Solution

The problem lies with

Element table = doc.select("table[class=local_table nbu_rate]").first();

Note that css selectors for class names include the dot .. Your selector should be

Element table = doc.select("table.local_table.nbu_rate").first();

Alternatively, only one class should do:

Element table = doc.select("table.local_table").first();

Edit:

Your second issue seems to be is with

Iterator<Element> ite = table.select("td[USD]").iterator();

The css selector "td[USD]" will look for a <td> element with an attribute called USD. This does not exist. You need to adapt your selector. USD is the inner HTML, not an attribute.

I guess you need to read the 3rd <td> to get the number value. However, to be meaningful you probably need to know the currency too...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top