Jsoup嵌套列表项中的值

时间:2013-04-03 17:43:30

标签: java jsoup

我有一个HTML页面,我正试图从中挖掘Logname值。我可以将所有li文本卡在一起作为一个字符串,但不是我想要的。我只想在li之后的</span> Logname的第二部分。有什么方法可以轻松搞定吗?凭借我所拥有的,我可以分裂并得到我想要的但似乎应该有更优雅的方式?

当前代码

Elements detail = mHtml.select ("div.alpha-first");


        for (Element items : detail)
        {
            Log.d (TAG, " label text " + items.text());

            detail.

            if (items.text().equals ("ACID"))
            {
                Log.d (TAG, " got ACID ");
            }

        }

HTML

<html>
<title>emp id chart</title>
<body>
<div class="alpha-first">
      <ul class="account-detail">
         <li><span class="label">ID</span>42</li>
         <li><span class="label">Logname</span>George</li>
         <li><span class="label">Surname</span>Glass</li>
         <li><span class="label">ACID</span>15</li>
         <li><span class="label">Dept</span>101348</li>
         <li><span class="label">Empclass</span>Echo</li>
      </ul>
      <p class="last-swipe">3 Apr 9:53</p><br>  </div>
   <div class="detail-last-loc">
      <p style="font-size: 8pt;">Current status</p>
      <p class="current-location">Bldg #23 South Lot</p>
      <p> current time 10:43 <br /></p>
      <div class="detail-extra">
         <p><a href="/empswipe/history/151034842">More</a> | <a href="/empswipe/history/151034842/3">3 Day History</a></p>
      </div>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:3)

根据我的理解,根据您的示例,您需要获取:<li><span class="label">Logname</span>George</li>,值:George

你真的不需要迭代,你可以直接得到它。我不会把这段代码称为优雅,但仍然是:

    //Select the <span> element the text "Logname"
    Elements select = mHtml.select(".account-detail span.label:contains(Logname)");

    //Get the element itself, since the select returns a list
    Element lognameSpan = select.get(0);

    //Get the <li> parent of the <span>
    Element parent = lognameSpan.parent();

    //Access the text node of the <li> directly since there is only one
    String logname = parent.textNodes().get(0).text();

希望它有所帮助。