Reputation: 15
The html code (not from my website, so I cant change it) looks like this:
<div id="resulttable">
<div class="dirlist">
<div class="stationcol" style="width:428px;">
<a href="http://whatever.com?id=xxx" title="Whatever" class="playbutton playimage" name="whatever" id="105867"></a>
<div class="videoBody">
<div class="gridModule">
<div class="surrogate">
<div id="thumbnail105867" class="thumbnail">
<a class="playbutton clickabletitle" name="whatever" id="105867" title="Whatever" href="http://whatever.com?id=xxx"> Bla </a>
</div></div></div></div></div></div></div>
Here is my Code:
Document doc = Jsoup.parse(result);
Elements hrefs = doc.select("div.stationcol a[href]");
StringBuilder links = new StringBuilder();
for (Element href : hrefs) {
links.append(href.text());
}
String httplinks = links.toString();
System.out.println("TEST: " + httplinks);
The output looks like:
I/System.out(10451): Link1http://www.whatever.c...Link2http://www.test.c...
What I really need is an ArrayList
that contains the Urls and maybe one separate ArrayList
that contains the Titles.
Can anyone help me please?
Upvotes: 1
Views: 4016
Reputation: 5538
Do you mean something like this?
ArrayList<String> titles = new ArrayList<String>();
ArrayList<String> urls = new ArrayList<String>();
Document doc = Jsoup.parse(result);
Elements links = doc.select("div.stationcol > a[href]");
for (Element e : links) {
titles.add(e.attr("title"));
urls.add(e.attr("href"));
}
System.out.println(titles);
System.out.println(urls);
This will output the contents of both ArrayLists in your sample code, e.g:
[Whatever]
[http://whatever.com?id=xxx]
Upvotes: 4