Wednesday, January 7, 2009

RSS Reader - in Java

What is RSS?

The initials "RSS" are used to refer to the following formats:

  • Rich Site Summary (RSS 0.91)
  • RDF Site Summary (RSS 0.9 and 1.0)
  • Really Simple Syndication (RSS 2.0)

RSS is a format for delivering regularly changing web content. Many news-related sites, weblogs and other online publishers syndicate their content as an RSS Feed to whoever wants it.

Reference: http://en.wikipedia.org/wiki/RSS

The Rome API

You could define RSS very simply by saying that it is an XML format that allows you to share information in a condensed manner; for example by publishing only a title, a description, and a link to a more detailed article.

Now, strictly speaking RSS is not a single format, but a family of formats. Among the many flavors of RSS are RSS 0.91 (in Netscape and Userland "sub-flavors"), RSS 0.92, RSS 0.93, RSS 0.94, RSS 1.0, and RSS 2.0. More recently Atom 1.0 has also entered the mix, introducing many subtle and non-so-subtle compatibility issues.

Although you can very well write your own RSS script directly (after all, it's just XML!), doing so forces you to choose, and stick to, one particular format. The issue is even trickier if you have to read and process external RSS feeds, which can come in a variety of formats. This is where an API like Rome comes in handy. In addition to being fluent in the many flavors of RSS, as you will see, the Rome API is easy to use and intuitive to understand, which helps to make coding more productive and code more maintainable.

Rome is an open source Java API for reading and publishing RSS feeds in a relatively format-neutral way. Originally developed by Sun and now a Java.net project, Rome is designed to provide a level of abstraction to mask the subtle differences between the various formats. This lets you concentrate on publishing and/or processing content, rather than wrestling with the particularities of each format. Rome was designed from the onset to be easy to use, flexible, and to support all existing RSS flavors.


Download ROME API here

ROME API documentation

Reference: http://www.javaworld.com/javaworld/jw-11-2007/jw-11-rome.html


Source Code:



URL feedUrl = new URL("http://www.indianetzone.com/rss/sports.xml");
URLConnection uc = feedUrl.openConnection();



SyndFeedInput input = new SyndFeedInput();


Reader rdr = new XmlReader(uc);


SyndFeed feed = input.build(rdr);


//Iterate through object to get details
List list = feed.getEntries();

System.out.println("Feed image="+feed.getImage());
if (feed.getImage()!=null)
{
SyndImageImpl image = (SyndImageImpl)feed.getImage();
String imageInfo = "Image url:"+image.getUrl()+"\n";
}
pw.println("");

for (int i=0 ; i < entry =" (SyndEntry)list.get(i);">author : "+entry.getAuthor()+"
");
//pw.println("\npublished : "+entry.getPublishedDate()+"
");
//pw.println("\nupdated : "+entry.getUpdatedDate()+"
");
pw.println("\ndescription : "+entry.getDescription().getValue()+"
");

}
pw.println("
");

}