Jsoup在第一个元素之后选择元素

时间:2015-10-09 09:17:23

标签: java android html jsoup

我想用jsoup解析一个html表。

我要解析的html页面的一部分:

SELECT [Id], [Title] FROM WorkItems WHERE State<>'Closed' AND AttachmentCount<>0

我如何选择所有&#34; tr&#34;在(包括)带有id&#34; RContents&#34;?的tr之后的元素 我尝试了<tr> <td class="dkHeading">A1</td> <td class="dkHeading">A2</td> <td class="dkHeading">A3</td> <td class="dkHeading">A4</td> <td class="dkHeading">A5</td> <td class="dkHeading">A6</td> <td class="dkHeading">A7</td> </tr> <tr id="RContents"> <td class="dkTextCenter">B1</td> <td class="dkTextCenter">B2</td> <td class="dkTextCenter">B3</td> <td class="dkTextLeft">B4</td> <td class="dkTextCenter">B5</td> <td class="dkTextCenter">B6</td> <td class="dkTextCenter">B7</td> </tr> <tr> <td class="dkTextCenter">C1</td> <td class="dkTextCenter">C2</td> <td class="dkTextCenter">C3</td> <td class="dkTextLeft">C4</td> <td class="dkTextCenter">C5</td> <td class="dkTextCenter">C6</td> <td class="dkTextCenter">C7</td> </tr> <tr> <td class="dkTextCenter">D1</td> <td class="dkTextCenter">D2</td> <td class="dkTextCenter">D3</td> <td class="dkTextLeft">D4</td> <td class="dkTextCenter">D5</td> <td class="dkTextCenter">D6</td> <td class="dkTextCenter">D7</td> </tr> ,但这确实无效。

2 个答案:

答案 0 :(得分:2)

您可以使用下一个兄弟选择器〜:

doc.select("tr[id=RContents] ~ tr");

答案 1 :(得分:0)

您可以选择tr Elements,然后选择loop。因为元素是按顺序你可以尝试这样的东西:

    Document document = Jsoup.parse("YOURHTML");

    Elements elements = document.select("tr");

    boolean start=false;
    for(Element e : elements){
        if(e.hasAttr("id") && e.attr("id").equals("RContents"))){   
            start=true;
        }

        if(start){
         //all tr elements including id=RContents and after
        }
    }