0

我正在尝试在下面的 html 中的 span 标记之后选择下一个节点值(编号 4)。我怎样才能做到这一点??

<tr valign="top">
    <td></td>
    <td><a href="#"> 1 </a></td>
    <td><a href="#"> 2 </a></td>
    <td><span> 3 </span></td>
    <td><a href="#"> 4 </a></td>
    <td><a href="#"> 5 </a></td>
    <td><a href="#"> 6 </a></td>
</tr>
4

1 回答 1

0
final String html = "<tr valign=\"top\">\n"
        + "    <td></td>\n"
        + "    <td><a href=\"#\"> 1 </a></td>\n"
        + "    <td><a href=\"#\"> 2 </a></td>\n"
        + "    <td><span> 3 </span></td>\n"
        + "    <td><a href=\"#\"> 4 </a></td>\n"
        + "    <td><a href=\"#\"> 5 </a></td>\n"
        + "    <td><a href=\"#\"> 6 </a></td>\n"
        + "</tr>";

Document doc = Jsoup.parse(html);

Element nextToSpan = doc.select("span").first().nextElementSibling();

解释:

doc.select("span") // Select the span-tags of doc
    .first()    // retrieve the first one
    .nextElementSibling();  // Get the element that's next to it

文档:http: //jsoup.org/cookbook/extracting-data/selector-syntax

于 2013-07-18T20:39:43.513 回答