简体   繁体   中英

Jsoup remove specific TR id

I try to remove a "TR" on a table with specific id, where I'm got from

String url = "http://citraslider.blogspot.com/2014/03/table-model.html";
Document doc = Jsoup.connect(url).get();
System.out.println(doc);

And print:

<table id="mt" border="0" cellpadding="2" cellspacing="0" width="100%" class="hbtbl">
    <tr id="1"><td class="aa"><div class="bb">11</div><a href="mailto:asd@yahoo.com" target="_blank"><b class="nme pn_std">bua</b></a>: ask 1 ?</td></tr>
    <tr id="2"><td class="a"><div class="b">12</div><a href="mailto:asd@yahoo.com" target="_blank"><b class="nme pn_std">bua</b></a>: ask 2 ?</td></tr>
    <tr id="3"><td class="aa"><div class="bb">13</div><a href="mailto:asd@yahoo.com" target="_blank"><b class="nme pn_std">bua</b></a>: ask 3 ?</td></tr>
    <tr id="-1"><td class="a"><div align="center"><a href="javascript:void(window.open('index.php?mid=1&btag=awe&i='+((cf.op)?cf.op:8304), 'archive', 'width=320,height=400,resizable=yes,scrollbars=yes'));">[prev]</a></div></td></tr>
</table>

Here is:

Elements elemen= doc.select("tr");
Elements el = doc.getElementsByAttributeValue("id", "-1");
el.remove(0);
System.out.println(el); // Here its work
for (Element e : elemen) {
    System.out.println(e.text()+":"+e.attr("id")); // But at this line still show [prev], tr id="-1" still show
    e.getElementsByAttributeValue("id", "-1").remove();
}

So, how i can remove "tr id=-1" on a loop results?

    <tr id="-1"><td class="a"><div align="center"><a href="javascript:void(window.open('index.php?mid=1&btag=awe&i='+((cf.op)?cf.op:8304), 'archive', 'width=320,height=400,resizable=yes,scrollbars=yes'));">[prev]</a></div></td></tr>

Just change your selector to exclude tr for id = -1

Elements elemen= doc.select("tr").not("tr#-1");

More information on the selector syntax here .

Code becomes,

Elements elemen= doc.select("tr").not("tr#-1");
        for (Element e : elemen) {
            System.out.println(e.text()+":"+e.attr("id"));

        }

Gives,

11bua: ask 1 ?:1
12bua: ask 2 ?:2
13bua: ask 3 ?:3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM