0

I have an HTML page that I'm trying to dig out the Logname value from. I can get all the li text jammed together as one string, but not quite what I want. I'd like just the second part of the li Logname after the </span>. Any way to easily get that? With what I have, I could do a split and get what I want but seems like there should be a more elegant way?

Current code

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 Answer 1

3

From what I understood, given your example, you would want to obtain from: <li><span class="label">Logname</span>George</li>, the value: George.

You really don't need to iterate, you can get it directly. I would not go so far as to call this code elegant, but still, here it is:

    //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();

Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that's just what I was wanting to do. My example code was a a little confusing as I was branching on ACID rather than Logname.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.