Friday, July 30

Creating a RSS Feed reader in Java using ROME


          As I was working on developing app for parsing/reading the RSS feed in Java, I sort of though about sharing my experience and here today I am going to guide you through detailed steps in creating a Java application for reading the RSS feed.

As I googled, I found two option either to use Java Utilities or another powerful library 
ROME ( Rss and atOM utilitiEs). And I selected ROME because it is open source Java tools for parsing, generating and publishing RSS and Atom feeds. The core feature of ROME is that the dependancy is only upon JDOM XML parser and supports parsing, generating and converting all of the popular RSS and Atom formats.
ROME includes parsers to process syndication feeds into SyndFeed instances. The SyndFeedInput class handles the parsers using the correct one based on the syndication feed being processed. The developer does not need to worry about selecting the right parser for a syndication feed, the SyndFeedInput will take care of it by peeking at the syndication feed structure.

You can download ROME
here https://rome.dev.java.net/dist/rome-1.0.jar
And JDOM XML parser
here http://www.jdom.org/dist/binary/jdom-1.1.1.zip


Now here is the sample java code:


import java.net.URL;
import java.util.Iterator;
import java.io.InputStreamReader;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

/**
 * It Reads and prints any RSS/Atom feed type.
 * 
 * @author kxhitiz
 *
 **/
public class rss {

    public static void main(String[] args) {
        boolean ok = false;
        
       SyndFeedInput input=new SyndFeedInput();
       URL feedUrl;
       SyndFeed feed;
       Iterator entries;
       
     try 
     {
        feedUrl= new URL("http://feeds.bbci.co.uk/news/rss.xml");
        feed = input.build(new XmlReader(feedUrl));
        entries = feed.getEntries().iterator();
        
      while (entries.hasNext())
     {
     SyndEntry entry= (SyndEntry) entries.next();
     System.out.println("URI: " + entry.getUri());
     System.out.println("Title: " + entry.getTitle());
     System.out.println("Description: " + entry.getDescription().getValue());
     System.out.println();
     }
     }
       
     catch (Exception ex)
     {}
        

        
    }

}

1 comment: