Tuesday, February 23, 2010

RSS - Really Simple Syndication

With RSS, it is possible to distribute up-to-date web content from one web site to several other web sites or other applications. RSS has been designed to show selected data.
There are several web sites which provide RSS feeds. A consumer can consume these RSS feeds from multiple web sites and can view a consolidated information in his application. If you are designing your own application and wondering how to consume these RSS feeds, then you are at right spot.
RSS feeds are nothing but XML data, so either you can write your own parser to parse this XML data or you use a third party library to parse it for you.
A RSS feed may look like:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">

<channel>
<title>RSS Test</title>
<link>http://www.rsstest.com</link>
<description>RSS sample file</description>
<item>
<title>RSS Introduction</title>
<link>http://www.rsstest.com/rssintro</link>
<description>RSS Introduction</description>
</item>
<item>
<title>RSS Java Code</title>
<link>http://www.rsstest.com/java</link>
<description>How to consume RSS in java</description>
</item>
</channel>
</rss>


In the following example, I have used informa.jar. A third party library, as I don't want to get into details of parsing XML.
I have used a feed from discovery site, which will display the top stories from the discovery site.

public class Client {
public static void main(String[] args) {
Client client = new Client();
client.readRSS();

}

public void readRSS() {
try {
ChannelIF channel = FeedParser.parse(new ChannelBuilder(), new URL("http://dsc.discovery.com/news/subjects/animals/xdb/topstories.xml"));
//Collection feeds = OPMLParser.parse(inpFile);

Collection collection = channel.getItems();
Iterator it = collection.iterator();
ItemIF itemIF;
while(it.hasNext()) {
itemIF = (ItemIF)it.next();

System.out.println("Title: " + itemIF.getTitle());
System.out.println("Link: " + itemIF.getLink());
System.out.println(itemIF.getDate());
System.out.println("Description: " + itemIF.getDescription());
}

System.out.println("Title: " + channel.getTitle());
System.out.println("Copyright: " + channel.getCopyright());



System.out.println("DONE");
}
catch(Exception e){
e.printStackTrace();
}
}
}

That's all.
Enjoy!!! Have Fun !!! :)