The Learner
The Learner

Reputation: 3917

Jsoup getting a hyperlink from li

I have

<div id = top1>
   <div id topsub1>
   <ul class="student">
   <li>
    <a href="/thomas">Tom</a>
    </li>
    </div>
   <div id topsub2>
   <ul class="student">
   <li>
    <a href="/thomas1">Tom1</a>
    </li>
    </div>
 </div>

I want to get the href and text So I did Elements xx= select (div div ul li)

when I do foreach xx for y and if I do

string1= y.text(); //This is printing Tom String2= y.attr("href") //This is always empty. I am not able to get /thomas? I also tried y.attr("a[href]"))

and also what is : doc.select(".studentnames > a");? does this mean that on the ID=studentnames get all the "a" correct???

Upvotes: 1

Views: 3250

Answers (2)

BachT
BachT

Reputation: 1068

This is your error:

1. Your first select only get div div ul li, so that Elements contains li tag(s) only. So you can do either of the following way:

Get each element

for (Element x: yy) {
  Element aTag = x.child(0);
  // or it can be aTag = x.select("a[href]").first();
  // Do your stuff here !
}

Get from the select query:

Elements yy = doc.select("div div ul li a[href]");

I want to mention that, please do not use Element.text() is to get the data inside tags, so in this case, it is the li. While debugging, you should use Element.html() or Elements.html().

2. About your concern:

and also what is : doc.select(".studentnames > a")? does this mean that on the ID=studentnames get all the "a" correct???

. is class, whereas # is id. Furthermore, > is direct child.

So your query means that: "return all the a tag which is a direct child of a tag which has class equals 'studentnames'"

For more specific about it, you should check Jsoup's Selector's document

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

What if you simply did Elements eles = doc.select("a[href]");? Also I believe that when using the attrib(...) method, you don't pass in the tag, just the attribute name itself.

edit:
You state:

the issue with doing doc.select("a[href]..there are many many attributes and hrefs..i want only that is next to student name..thats why i am doing immediately after I do text so that I can get - name of student and his website

Then refine result returned by your select by calling multiple selects sequentially or chaining them:

Elements eles = doc.select("ul.student").select("a[href]");

or perhaps (I've never done this):

Elements eles = doc.select("ul.student a[href]");

or even:

Elements eles = doc.select("div > div > ul.student > li > a[href]");

Upvotes: 2

Related Questions