devcoder
devcoder

Reputation: 1685

Parse an RSS feed for Android app ? Is jsoup the answer?

I am reading the Head First Android development book. In the third chapter where they try to make an app from NASA RSS feed from here .
In the book the author uses SAX parser for Java. I looked online and some of the answers here on SO suggest that SAX is outdated and there are newer solutions.

However I'm not sure what the easier to use ones are for Java. I have used Nokogiri for Ruby and something similar would be awesome. I looked at jsoup and it looked alright, but I am wondering what suggestions you guys might have.

Upvotes: 0

Views: 2565

Answers (5)

Koushik Sarkar
Koushik Sarkar

Reputation: 498

The code on the chapter 3 halts because Android doesn't support networking in it's main thread.

So you can use any parser like XmlPullParser but make sure you do the networking(downloading the feed etc.) off of it's main thread. You can use AsyncTask to take the networking outside the main thread.. or create a new Thread() and do the networking in that thread (Recommended)

Actually, in the 4th chapter they actually DID create a new thread to do the networking. So if you use the chapter4 code instead then it will work.

Another problem you might face is of OutOfMemoryError because Nasa daily images are really big these days. So you'll have to decode the image with inSampleSize. You can check other questions on decoding an image right to get what you want. Good luck. ))

Upvotes: 1

BalusC
BalusC

Reputation: 1108692

Since version 1.6.2, Jsoup officially also supports XML parsing. This thus allows you to parse XML and select elements using jQuery-like CSS selectors. To create a XML document with Jsoup, you need the following instead of Jsoup#parse() method:

Document document = Parser.xmlParser().parseInput(xmlString, "");
// ...

This way the input won't implicitly be treated as HTML5 (so, no auto-included <html><head> tags and so on).

Upvotes: 0

sjwoodr
sjwoodr

Reputation: 326

I'm a big fan of Jsoup. I only recently started using it and its amazing. I used to write some super hairy regex patterns to do pattern matching with because I wanted to avoid SAX like the plague... and that was quite tedious as you can imagine. Jsoup let me parse out specific items from a <table> in just a few lines of code.

Let's say I want to take the first 7 rows of a table where the <tr class=...> is GridItem or GridAltItem. Then, lets say we want to print the 1st, 2nd, and 3rd columns as text and then the first <a href> link that appears in the row. Sounds goofy, but I had to do this and I can do this easily:

String page = "... some html markup fetched from somewhere ...";
Document doc = Jsoup.parse(page);
for(int x=0; x< 7; x++) {
  Element gridItem = doc.select("tr[class$=Item]").select("tr").get(x);
  System.out.println("row: " + gridItem.select("td").get(0).text() + " " + gridItem.select("td").get(1).text() + " " + gridItem.select("td").get(4).text() + " " + gridItem.select("a").get(0).attr("href"));
}

Its that simple with Jsoup. Make sure you add the Jsoup jar file to your project as a library and import those classes which you need: you don't want to import the wrong Document or Element class...

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

Enjoy!

Upvotes: 0

Jonathan
Jonathan

Reputation: 11

I'm the author of Head First Android Development, so just wanted to chime in with a few thoughts. SAX is definitely a bit cumbersome, but straightforward and was built into Android for a while (hence the decision to use that in the book). I'm also a rails developer and I'm a big fan of nokogiri and use it often. Looking at jsoup, I could definitely see that being useful. That said, I haven't tried it out, so I can't give any first hand experience with it.

Another option to look at is the XML PullParser built into Android. It's still pretty SAX-like, but a bit more full featured.

Hope this helps.

Upvotes: 1

Programmer
Programmer

Reputation: 5400

I think SAX is a default way to acheive it, no boundations on trying something new though :)

Upvotes: 0

Related Questions