Tuesday, March 10, 2009

Consuming RSS Feeds with Groovy/Grails

Building off of my recent post about RSS Feed parsing using the ROME Library, I had an idea for a fun application to build using Grails. The first part of this application, which I'll share when I am done, is the part where we read the feed. Now, the last time I did this in Java, it was pretty easy to do, but man, this is just a little bit better:
    def readFeed( url )
{
def xmlFeed = new XmlParser().parse(url);

def feedList = []

(0..< xmlFeed.channel.item.size()).each {

def item = xmlFeed.channel.item.get(it);
RSSFeed feed = new RSSFeed( item.title.text(), item.link.text(),
item.description.text(), item.pubDate.text() )
feedList << feed
}

feedList
}
Yep, that's it. One line to pull back the feeds. The iterator, and one line to create my RSSFeed object. Then add the feed to a list, and return the list to your controller. In 25 minutes, I have a feed reader application that's basically functional, taking a feed as an input, and returning a page that displays the post title linked to the post, and the posts contents and date. All of life should be this easy.
blog comments powered by Disqus