从JSOUP中的表中选择tr

时间:2015-11-30 05:54:29

标签: java html jsoup

我想从表中获取TR标签,就像我解释的那样。但是当我使用

Elements elementObj = doc.select("table").select("tr");

表格中存在所有TR标签。但我只想将父TR标签作为我突出显示的元素,而不是子TR标签。有人帮我请!

<table>
   <tr>//This tr as Element
      <td>
         <table>
             <tr>
                !!!NOT these tr
             </tr>
         </table>
      </td>
   </tr>           
   <tr>//This tr as Element
      <td>
         <table>
             <tr>
                !!!NOT these tr
             </tr>
         </table>
      </td> 
   </tr>

2 个答案:

答案 0 :(得分:1)

我使用以下代码解决了这个问题:

Elements elementobj = doc.select("table>tr");

这将成为第一级孩子。

答案 1 :(得分:0)

使用children(),它只为您提供直接节点。所以像这样使用它:

Elements elementObj = doc.select("table").first().children().select("tr");

假设您只有一个table元素,如果有多个元素,则应首先获取所有table元素,然后迭代它们并在每个元素上调用children()

相关问题