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
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();
}
}
}
Enjoy!!! Have Fun !!! :)
2 comments:
If you have a lot of RSS to parse you might want to use the latest XML parsing libs, one worth mentioning is vtd-xml
http://vtd-xml.sf.net
Thanks a lot for the info buddy. Will look into it and put up a post for vtd-xml.
Post a Comment