Reputation: 1
i am developing android application using JSOUP for parsing HTML.
i have HTML syntax
<div class='wrapper'>
<div style='margin:7px;'>
<div class='box' style='height:595px'>
<div class='boxtitlebox'>
<div class='boxtitle'><h4>13 RECENT CHORDS</h4></div><div class='clear'></div>
</div>
<div class='listitem'><a href='http://www.chordfrenzy.com/chord/9742/ungu-apa-sih-maumu-kord-lirik-lagu'>
<div class='subtitle'>Chord Ungu</div>
<div class='title'>Apa Sih Maumu</div>
</a></div>
<div class='listitem'><a href='http://www.chordfrenzy.com/chord/6826/slank-boneka-tersayang-kord-lirik-lagu'>
<div class='subtitle'>Chord Slank</div>
<div class='title'>Boneka Tersayang</div>
</a></div>
<div class='listitem'><a href='http://www.chordfrenzy.com/chord/6751/ari-lasso-rayuan-gombal-kord-lirik-lagu'>
<div class='subtitle'>Chord Ari Lasso</div>
<div class='title'>Rayuan Gombal</div>
</a></div>
</div>
</div>
</div>
Now, i am confuse how can i get each ahref, subtitle and title above?
i need it to fill my array like this
String[] link=["http://www.chordfrenzy.com/chord/9742/ungu-apa-sih-maumu-kord-lirik-lagu","http://www.chordfrenzy.com/chord/6826/slank-boneka-tersayang-kord-lirik-lagu","http://www.chordfrenzy.com/chord/6751/ari-lasso-rayuan-gombal-kord-lirik-lagu"];
String[] subtitile=["Chord Ungu","Chord Slank","Chord Ari Lasso"];
String[] title=["Apa Sih Maumu","Boneka Tersayang","Rayuan Gombal"];
any ide?
Upvotes: 0
Views: 8498
Reputation: 271
Document document = Jsoup.parse(html);
Elements hrefElements = document.select("div.listitem");
String[] links = new String[hrefElements.size()];
String[] title = new String[hrefElements.size()];
String[] subtitle = new String[hrefElements.size()];
for(int i=0;i<hrefElements.size();i++)
{
links[i] = hrefElements.get(i).getElementsByTag("a").attr("href");
title[i] = hrefElements.get(i).getElementsByClass("title").text();
subtitle[i] = hrefElements.get(i).getElementsByClass("subtitle").text();
}
for(int j=0;j<hrefElements.size();j++)
{
System.out.println("Links: "+links[j]);
System.out.println("Title: "+title[j]);
System.out.println("SubTitle: "+subtitle[j]);
}
Upvotes: 1
Reputation: 1
I think that the ArrayList
structure is better than an array of Strings
Elements links = doc.getElementsByClass("listitem");
Elements subtitles = doc.getElementsByClass("subtitle");
Elements titles = doc.getElementsByClass("title");
List<String> link = new ArrayList<String>();
List<String> subtitile = new ArrayList<String>();
List<String> title = new ArrayList<String>();
for (Element e : links) {
String href = e.getElementsByAttribute("href").first().attr("href");
link.add(href);
}
for (Element e : subtitles) {
String s = e.text();
subtitile.add(s);
}
for (Element e : titles) {
String s = e.text();
title.add(s);
}
Upvotes: 0
Reputation: 25350
In general you should prefer the Selector API instead of DOM (getElementsByX
)
Here's an example:
Document doc = Jsoup.parse(html);
// Links
List<String> links = new ArrayList<>();
for( Element element : doc.select("a[href]") )
{
links.add(element.attr("href"));
}
// Subtitles
List<String> subtitles = new ArrayList<>();
for( Element element : doc.select("div[class=subtitle]") )
{
subtitles.add(element.text());
}
// Titles
List<String> titles = new ArrayList<>();
for( Element element : doc.select("div[class=title]") )
{
titles.add(element.text());
}
Elements are selected by tag and attribute, if the tags differ or are not relevant you can remove them (eg. [class=title]
instead of div[class=title]
). Take a look at the Selector API (link above) for some more tipps.
Upvotes: 5