Cloakd
Cloakd

Reputation: 165

jsoup retrieving a specific table from the header

i have been working on this for a while and just cant seem to work out how to get the correct table that corresponds to the header it has. the tables are split up into sections which i can retrieve however inside the section is a header with the title of the table. i need to find the section with the header that matches a string and then pull the data from it. I'm fine with getting the data out of the table its just getting the correct section for the table

HTML extract of the section:

<section class="blueTab">
            <header><h2>Energy</h2></header> //<----- THE HEADER I NEED TO MATCH TO
            <table class="infoTable">
                <tr><th>Model</th><th>0-60 mph</th><th>Top Speed</th><th>BHP</th><th></th></tr>

                        <tr>
                            <td><p>1.4i 16V Energy 5d</p></td>
                            <td><p>12.8 secs</p></td>
                            <td><p>111 mph</p></td>
                            <td><p>88 bhp</p></td>
                        </tr>

                        <tr class="alternate">
                            <td><p>1.6i 16V Energy 5d</p></td>
                            <td><p>11.5 secs</p></td>
                            <td><p>115 mph</p></td>
                            <td><p>103 bhp</p></td>
                        </tr>

                        <tr>
                            <td><p>1.8i VVT Energy 5d Auto</p></td>
                            <td><p>10.7 secs</p></td>
                            <td><p>117 mph</p></td>
                            <td><p>138 bhp</p></td>
                        </tr>

                        <tr class="alternate">
                            <td><p>1.3 CDTi 16V Energy 5d</p></td>
                            <td><p>12.8 secs</p></td>
                            <td><p>107 mph</p></td>
                            <td><p>88 bhp</p></td>
                        </tr>

            </table>

            <div class="fr topMargin">
                <div id="ctl00_contentHolder_topFullWidthContent" class="modelEnquiry">


<div id="ctl00_contentHolder_topFullWidthContent" class="buttonLinks">

</div>
<div class="cb"><!----></div>

</div>
            </div>
            <div class="cb"><!----></div>
    </section>

Im guessing i will have to use doc.getElementsByClass("blueTab") in a for loop and for each element see if h2 equals the string im looking for, i am just not sure how to implement this

Upvotes: 0

Views: 931

Answers (1)

Niranjan
Niranjan

Reputation: 1844

This should solve your problem

Document doc = Jsoup.parse(input, "UTF-8");
    Elements elem = doc.select(".blueTab header h2");
    for (Iterator<Element> iterator = elem.iterator(); iterator.hasNext();)
    {
        Element element = iterator.next();
        if (element.text().equals("Energy")) // your comparison  text
        {
            Element tableElement = element.parent().nextElementSibling(); //Your got the expected table Element as per your requirement
        }

    }

Upvotes: 1

Related Questions