Pages

Reading RSS Feed in Groovy

The more I explore, the more I love groovy. Last week while I was demonstrating a few colleagues of mine, that how simple things are really dead-simple in groovy, I wrote this small code that fetches the rss feed and prints the headlines. Just thought of sharing with all.
// setup proxy, if any
System.properties.putAll(["http.proxyHost":"your.proxy", "http.proxyPort":"8080"])             

// define the url, change it to whatever rss url you like
def url = "http://news.google.com/news?ned=us&topic=h&output=rss"

def rss = new XmlSlurper().parse(url) 

println rss.channel.title
rss.channel.item.each {
    println "- ${it.title}"
}
That's it! You have headlines in your output. How can you not love the brevity yet expressiveness of this code:)

1 comment:

Anonymous said...

But, what about this kind of link?

http://www.cartacapital.com.br/internacional/internacional/atom.xml