Creating RSS using Java

I wanted to create RSS feeds for karensrecipes.com using Java. I did my ‘research‘, came to this page: Ben Hammersley.com: Java RSS libraries and then used the RSS4j library to create a servlet that serves up dynamic RSS feeds of the 10 most recently created recipes per category (samples: Breakfast, Soup, Barbeque..).

They syntax is pretty simple, you get an RssDocument and set which version you want to use (RSS 1.0, .9 or .91):

RssDocument doc = new RssDocument();
doc.setVersion(RssDocument.VERSION_10);

and then create a RssChannel object and add that to the RssDocument:

RssChannel channel = new RssChannel();
channel.setChannelTitle("Karens Recipes | Most Recent");
channel.setChannelLink("http://www.karensrecipes.com/3/Soup/default.jsp");
channel.setChannelDescription("The 10 most recently added recipes in the soup category.");
channel.setChannelUri("http://www.karensrecipes.com/rss/?categoryid=3");
doc.addChannel(channel);

Next, you’ll retrieve the items using a database, the file system, etc… and add each item as a RssChannelItem:

// connect to the datasource
// iterate over something (db? vector?...)
RssChannelItem item = new RssChannelItem();
item.setItemTitle(label);
item.setItemLink(link);
item.setItemDescription(description);
channel.addItem(item);

and then finally, using the RssGenerator class, call the generateRss() method, in this case I’m sending the output to a Servlet PrintWriter:

PrintWriter out = response.getWriter();
RssGenerator.generateRss(doc, out);

You could just as easily write it to a file:

File file = new File("/opt/data/rss.xml");
try{
RssGenerator.generateRss(doc, file);
System.out.println("RSS file written.");
}
catch(RssGenerationException e){
e.printStackTrace();
}

Simple. Easy to use.

6 thoughts on “Creating RSS using Java”

  1. Does anyone have contact info for Jon Churchill, the author of rss4j? I’ve looked all over his site and through his downloads, and there is nothing to be found. So how can you suggest any improvements to his open source code?

    That said, this is a really nice little library, and I don’t want to fork my own version. I just need to be able to delete items from a channel. So items in RssChannel should be protected, or he should expose a remove method.

    Thanks, J

  2. ihad read the article it nice and very usefull for me because i am going to do the RSS project using j2se and XML, so i need key words for to start my project and books name please reply for this

  3. I read this article. This is very nice to me. I create RSS reader using java. WHat are the requirements? I have jdk1.2.0, Tomcat 5.0.
    I downloaded ur RSS4j.jar.
    Anythisng else need for create RSS reader

  4. Aaron, this is a very nice article.Please can you elaborate more clearly with the complete code and example?
    I will be eternally grateful

    Regards

  5. churchillobjects.rss4j.generator.RssGenerationException: Invalid URI format: must be ‘http:’, ‘https:’, ‘ftp:’, or ‘mailto:’ – Link
    at churchillobjects.rss4j.generator.RssGenerator.validateUri(RssGenerator.java:456)
    at churchillobjects.rss4j.generator.RssGeneratorImpl090.handleItemLink(RssGeneratorImpl090.java:204)
    at churchillobjects.rss4j.generator.RssGeneratorImpl100.handleDocumentItems(RssGeneratorImpl100.java:200)
    at churchillobjects.rss4j.generator.RssGeneratorImpl100.finishDocument(RssGeneratorImpl100.java:121)
    at churchillobjects.rss4j.generator.RssGenerator.writeRssDocument(RssGenerator.java:231)
    at churchillobjects.rss4j.generator.RssGenerator.writeRssDocument(RssGenerator.java:286)
    at churchillobjects.rss4j.generator.RssGenerator.generateRss(RssGenerator.java:137)
    at com.soco.test.Test.main(Test.java:30)

Leave a Reply

Your email address will not be published. Required fields are marked *