{"id":492,"date":"2003-09-07T19:21:56","date_gmt":"2003-09-07T23:21:56","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=492"},"modified":"2003-09-07T19:21:56","modified_gmt":"2003-09-07T23:21:56","slug":"creating-rss-using-java","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/09\/07\/creating-rss-using-java\/","title":{"rendered":"Creating RSS using Java"},"content":{"rendered":"<p>I wanted to create RSS feeds for <a href=\"http:\/\/www.karensrecipes.com\">karensrecipes.com<\/a> using Java.  I did my &#8216;<a href=\"http:\/\/www.google.com\/search?sourceid=navclient&amp;ie=UTF-8&amp;oe=UTF-8&amp;q=java+rss\">research<\/a>&#8216;, came to this page: <a href=\"http:\/\/www.benhammersley.com\/archives\/004124.html\">Ben Hammersley.com: Java RSS libraries<\/a> and then used the <a href=\"http:\/\/www.churchillobjects.com\/c\/13005.html\">RSS4j<\/a> library to create a servlet that serves up dynamic RSS feeds of the 10 most recently created recipes per category (samples: <a href=\"http:\/\/www.karensrecipes.com\/1\/Breakfast\/default.jsp\">Breakfast<\/a>, <a href=\"http:\/\/www.karensrecipes.com\/3\/Soup\/default.jsp\">Soup<\/a>, <a href=\"http:\/\/www.karensrecipes.com\/22\/Barbeque\/default.jsp\">Barbeque<\/a>..). <\/p>\n<p>They syntax is pretty simple, you get an <a href=\"http:\/\/cephas.net\/docs\/rss4j\/churchillobjects\/rss4j\/RssDocument.html\">RssDocument<\/a> and set which version you want to use (RSS <a href=\"http:\/\/cephas.net\/docs\/rss4j\/churchillobjects\/rss4j\/model\/RssVersion.html#VERSION_10\">1.0<\/a>, <a href=\"http:\/\/cephas.net\/docs\/rss4j\/churchillobjects\/rss4j\/model\/RssVersion.html#VERSION_90\">.9<\/a> or <a href=\"http:\/\/cephas.net\/docs\/rss4j\/churchillobjects\/rss4j\/model\/RssVersion.html#VERSION_91\">.91<\/a>):<br \/>\n<code><br \/>\nRssDocument doc = new RssDocument();<br \/>\ndoc.setVersion(RssDocument.VERSION_10);<br \/>\n<\/code><br \/>\nand then create a <a href=\"http:\/\/cephas.net\/docs\/rss4j\/churchillobjects\/rss4j\/RssChannel.html\">RssChannel<\/a> object and add that to the RssDocument:<br \/>\n<code><br \/>\nRssChannel channel = new RssChannel();<br \/>\nchannel.setChannelTitle(\"Karens Recipes | Most Recent\");<br \/>\nchannel.setChannelLink(\"http:\/\/www.karensrecipes.com\/3\/Soup\/default.jsp\");<br \/>\nchannel.setChannelDescription(\"The 10 most recently added recipes in the soup category.\");<br \/>\nchannel.setChannelUri(\"http:\/\/www.karensrecipes.com\/rss\/?categoryid=3\");<br \/>\ndoc.addChannel(channel);<br \/>\n<\/code><br \/>\nNext, you&#8217;ll retrieve the items using a database, the file system, etc&#8230; and add each item as a <a href=\"http:\/\/cephas.net\/docs\/rss4j\/churchillobjects\/rss4j\/RssChannelItem.html\">RssChannelItem<\/a>:<br \/>\n<code><br \/>\n\/\/ connect to the datasource<br \/>\n\/\/ iterate over something (db? vector?...)<br \/>\n   RssChannelItem item = new RssChannelItem();<br \/>\n   item.setItemTitle(label);<br \/>\n   item.setItemLink(link);<br \/>\n   item.setItemDescription(description);<br \/>\n   channel.addItem(item);<br \/>\n<\/code><br \/>\nand then finally, using the <a href=\"http:\/\/cephas.net\/docs\/rss4j\/churchillobjects\/rss4j\/generator\/RssGenerator.html\">RssGenerator<\/a> class, call the generateRss() method, in this case I&#8217;m sending the output to a Servlet PrintWriter:<br \/>\n<code><br \/>\nPrintWriter out = response.getWriter();<br \/>\nRssGenerator.generateRss(doc, out);<br \/>\n<\/code><br \/>\nYou could just as easily write it to a file:<br \/>\n<code><br \/>\nFile file = new File(\"\/opt\/data\/rss.xml\");<br \/>\ntry{<br \/>\n  RssGenerator.generateRss(doc, file);<br \/>\n  System.out.println(\"RSS file written.\");<br \/>\n}<br \/>\ncatch(RssGenerationException e){<br \/>\n  e.printStackTrace();<br \/>\n}<\/p>\n<p>Simple.  Easy to use.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to create RSS feeds for karensrecipes.com using Java. I did my &#8216;research&#8216;, 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 &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/09\/07\/creating-rss-using-java\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Creating RSS using Java<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,3,4,10],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/492"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=492"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/492\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}