Michal
Michal

Reputation: 89

Android with Jsoup using tables

Hey Guys I am just starting on Jsoup and have a small problem with a table. I am trying to parse car details from this website http://mywheels.ie/car-history-check/free-car-check-results/?VRN=00c31865 but don't really know how to do it. Could somebody tell me how to address the table and copy at least one element from it ? thanks in advance

Elements table = doc.select("table");
Elements row = doc.select("table[width=\"100%\"] [cellspacing=\"0px\"] [cellpadding=\"0px\"]");
Iterator<Element> iterator = row.listIterator();
while(iterator.hasNext())
{
        Element element = iterator.next();
        String id = element.attr("id");
        String classes = element.attr("class");
        String value = element.text();
        System.out.println("Id : " + id + ", classes : " + classes+ ", value : " + value);
}

Upvotes: 0

Views: 635

Answers (1)

Peter Ambruzs
Peter Ambruzs

Reputation: 8213

I can recommend you to create a JAVA project to test Jsoup as it is much quicker that way. I completely restructured your code. I used descriptive variable names to easier understanding. Here is the code:

    Document doc;
    try {
        doc = Jsoup.connect("http://mywheels.ie/car-history-check/free-car-check-results/?VRN=00c31865").get();

        Element containingDiv = doc.select(".free-vehicle-report-topDiv").first();
        Elements table = containingDiv.select("table");
        Elements rows = table.select("tr");

        for (Element row : rows) {
            System.out.println("label: "+row.child(0).text()+", value:"+row.child(1).text());
            // LOG.i("label: "+row.child(0).text()+", value:"+row.child(1).text());
        }


    } catch (IOException e) {
        e.printStackTrace();
    }

I tested in JAVA also, In android you may comment out the Log.i method call instead the System.out.println. It is not so difficult. Good luck.

Upvotes: 1

Related Questions