Android jsoup выбирает ячейки таблицы

Привет, я пытаюсь получить некоторую информацию с веб-сайта (http://omhc.nl/site/default.asp?Option=10017&m=1). Структура таблицы такая:

<tr>
    <td colspan="4" style="border-bottom: 1px solid rgb(0, 0, 0);" width="100%">donderdag 19 april 2012&nbsp;</td>
</tr>
<tr>
    <td width="5%">&nbsp;</td>
    <td width="25%">17:00 - 22:00&nbsp;</td>
    <td bgcolor="" width="40%">KM</td>
    <td width="6%">Barhoofd&nbsp;</td>
</tr>
<tr>
    <td colspan="4" style="border-bottom: 1px solid rgb(0, 0, 0);" width="100%">vrijdag 20 april 2012&nbsp;</td>
</tr>
<tr>
    <td width="5%">&nbsp;</td>
    <td width="25%">16:30 - 19:30&nbsp;</td>
    <td bgcolor="" width="40%">Ouders/verzorgers van VL</td>
    <td width="6%">Bardienst&nbsp;</td>
</tr>
<tr>
    <td width="5%">&nbsp;</td>
    <td width="25%">16:30 - 19:30&nbsp;</td>
    <td bgcolor="" width="40%">Ouders/verzorgers van AvdN</td>
    <td width="6%">Bardienst&nbsp;</td>
</tr>
<tr>
    <td width="5%">&nbsp;</td>
    <td width="25%">16:30 - 21:00&nbsp;</td>
    <td bgcolor="" width="40%">EdK</td>
    <td width="6%">Barhoofd&nbsp;</td>
</tr>
<tr>
    <td width="5%">&nbsp;</td>
    <td width="25%">21:00 - 23:00&nbsp;</td>
    <td bgcolor="" width="40%">FK</td>
   <td width="6%">Barhoofd&nbsp;</td>
</tr>
<tr>
    <td width="5%">&nbsp;</td>
    <td width="25%">23:00 - 00:00&nbsp;</td>
    <td bgcolor="" width="40%">SW</td>
    <td width="6%">Barhoofd&nbsp;</td>
</tr>

Мой код:

package maartenbrakkee.bardienst.omhc;

import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;

public class BardienstActivity extends Activity {
    TextView tv;
    static final String URL = "http://omhc.nl/site/default.asp?Option=10017&m=1";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv = (TextView)findViewById(R.id.tv);
        tv.setSingleLine(false);

        try {
            tv.setMovementMethod(new ScrollingMovementMethod());
            tv.setText(getBarschema());
        } catch (Exception ex) {
            tv.setText("Error");
        }
    }

    protected String getBarschema() throws Exception {
        String result = "";
        Document document = Jsoup.connect(URL).get();

        Elements dagen = document.select("tr:has(td[width=100%]) + tr:has(td[width=40%])");

        // Dag
        String[] dag = new String[dagen.size()];
        int i = 0;
        for (Element dagen1 : dagen) {
            dag[i++] = dagen1.text() + "\n";
        }

        return dag[3];
    }
}

Я хотел бы получить за день [0]:

donderdag 19 april 2012 + "\n" + 17:00 - 22:00 KM Barhoofd

и день[1]:

vrijdag 20 april 2012 + "\n" + 16:30 - 19:30  Ouders/verzorgers van VL Bardienst "\n" + 16:30 - 19:30 Ouders/verzorgers van AvdN Bardienst + "\n" + 16:30 - 21:00 EdK Barhoofd + "\n" + 21:00 - 23:00   FK Barhoofd + "\n" 23:00 - 00:00 SW Barhoofd

но все, что я получаю, это только 17:00 - 22:00 KM Barhoofd в течение дня [0]. Как выбрать правильные ячейки (от первой tr td[width:100%] до следующей tr td[width:100%])?


person Waarten    schedule 17.04.2012    source источник


Ответы (1)


Я немного изменил ваш метод getBarSchema и, в основном, ваш селектор:

static protected List<String> getBarschema(String URL) throws Exception {

    Document document = Jsoup.connect(URL).get();

    // New Selector
    Elements dagen = document.select("div.content table tr td");

    // List better than array in this case
    List<String> dag = new ArrayList();   

    String line = "";
    for (Element dagen1 : dagen) {                              

        String width = dagen1.attr("width");      
        if(width.equals("100%") && !line.equals("")){
            dag.add(line);
            line ="";
        }    
        line += dagen1.text() + "\n";
    }        
    return dag;
}
person Rodri_gore    schedule 17.04.2012