Reputation: 162
Recently started programming Android Java (Eclipse), Im trying to make a simple reader app using jsoup.
Ive got html like this;
<article id="id" class="artikel">
<h1>Title</h1>
<p>paragraph 1</p>
<p>paragraph 2</p>
<p>paragraph 3</p>
</article>
<article id="id">
<p>comment1</p>
</article>
<article id="id">
<p>comment2</p>
</article>
Amounts of paragraphs is variable. The amount of comments as well. I want to get all the paragraphs within the article, none of the comments. The real article is always the first article tag, so Im using first() in combination with a wildcard to get it.
Here is the method Im using;
public String GetArticleBody(Document adoc)
{
//Document totalbody = (Document)adoc.select("article *").first();
//Element totalbody = adoc.select("article *").first();
//Elements paragraphs = adoc.select("article * > p);
Elements paragraphs = adoc.select(".article* p");
String body = "test";
for (Element p : paragraphs)
{
body = StringAttacher(body, p.text());
}
System.out.println(body);
return body;
}
As you can see Ive been fooling around with the methods from the cookbook and a few I found on SOF. From all of these methods all Ive ever gotten back is just the word test or nothing at all.
Could someone point me in the right direction to get those paragraphs?
Upvotes: 4
Views: 2719
Reputation: 31280
The issue you have is using the wrong selector in your first statement.
.
is the "class" selector, so you either hav "article" speller wrong, or have a .
when you shouldn't.
Try this instead:
public String GetArticleBody(Document adoc)
{
//Document totalbody = (Document)adoc.select("article *").first();
//Element totalbody = adoc.select("article *").first();
//Elements paragraphs = adoc.select("article * > p);
Elements paragraphs = adoc.select("article").first().select("p");
String body = "test";
for (Element p : paragraphs)
{
body = StringAttacher(body, p.text());
}
System.out.println(body);
return body;
}
This will get you the paragraphs in the first article.
Also, it often helps to remember that the jsoup selectors are the same as the ones used in CSS selectors (and a sub-set of the jQuery selectors). Any knowledge you have from those other areas is directly useable with jsoup.
Upvotes: 2