Create a RSS feed (part 1)Tag(s): XML
RSS is a method to syndicate content on the web. This is done by creating an XML document which summarizes specific content such as news, blog posts or comments or any informations. There are many versions but the 2 most used are RSS 2.0 and ATOM.
RSS 2.0
The official specification of RSS 2.0 : http://cyber.law.harvard.edu/rss/rss.html
A typical RSS XML file looks like :
<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet title="XSL_formatting" type="text/xsl" href="feed.xsl"?> <rss version="2.0"> <channel> <title>Real's HowTo</title> <description>Updates of useful code examples for Java</description> <link>http://www.rgagnon.com/howto.html</link> <pubDate>23 May 2007 00:00:00 GMT</pubDate> <image> <title>Real's HowTo</title> <width>144</width> <height>41</height> <link>http://www.rgagnon.com/howto.html</link> <url>http://www.rgagnon.com/images/realhowto-left.jpg</url> </image> <item> <title>JS: Resize an IFRAME based on its content</title> <description>Using the onLoad event, resize the IFRAME</description> <link>http://www.rgagnon.com/jsdetails/js-0129.html</link> <pubDate>23 May 2007 00:00:00 GMT</pubDate> <guid>http://www.rgagnon.com/jsdetails/js-0129.html</guid> </item> </channel> </rss>
Atom
Atom was an answer to the incompatibilites between all the RSS versions. Adopted by the IETF, the official specification is RFC4287, http://www.ietf.org/rfc/rfc4287.
A typical ATOM feed looks like :
<?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <title>Example Feed</title> <subtitle>A subtitle.</subtitle> <link href="http://example.org/"/> <updated>2003-12-13T18:30:02Z</updated> <author> <name>John Doe</name> <email>johndoe@example.com</email> </author> <id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id> <entry> <title>Atom-Powered Robots Run Amok</title> <link href="http://example.org/2003/12/13/atom03"/> <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> <updated>2003-12-13T18:30:02Z</updated> <summary>Some text.</summary> </entry> </feed>
Creating a feed
While you can create the XML file using a regular text editor, it's more interesting to do it from code. You can do it using regular XML library and make sure that all the required tags are there or use special tools which encapsulate the complexity of generating a valid feed. We will look at two of them.
Creating a feed with Apache Commons Digester
The org.apache.commons.digester is a package designed manipulating XML file. In the first release there are a few classes to deal with RSS feed, it was the org.apache.commons.digester.rss package. However, in the latest version, the binaries of this package is no longer distributed. It's still possible to build it from the examples but it's no longer part of the official "Commons Digester".
Look at the javadoc and the code at http://jsourcery.com/api/apache/jakarta/commons/digester/1.7/org/apache/commons/digester/rss/package-summary.html
So you need the main Commons Digester plus, as usual with Apache software, you have depencies with the BeanUtils v1.7 and Commons Logging
Download the required jars at http://jakarta.apache.org/commons/digester/
Don't forget the famous commons-digester-rss.jar, you can build it from the Commons Digester examples jar or download it from here.
import org.apache.commons.digester.rss.Channel; import org.apache.commons.digester.rss.Item; import org.apache.commons.digester.rss.RSSDigester; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.text.SimpleDateFormat; class CreateRSSFeedUsingCommons { public static void main(String args[]) { SimpleDateFormat formatter= new SimpleDateFormat("dd MMM yyyy HH:mm:ss Z"); String today = formatter.format(new Date()); Channel newChannel = new Channel(); newChannel.setCopyright("(c)Real Gagnon 2007"); newChannel.setDescription("Useful Java code examples"); newChannel.setLink("http://www.rgagnon.com/howto.html"); newChannel.setLanguage("en"); newChannel.setPubDate(today); Item item = new Item(); item.setTitle("Real's HowTo"); item.setLink("http://www.rgagnon.com/java-details/"); item.setDescription("Cool java snippet!"); newChannel.setPubDate(today); newChannel.addItem(item); try { FileOutputStream fout = new FileOutputStream("feed.xml"); newChannel.render(fout); fout.close(); } catch (IOException e) { e.printStackTrace(); } } }
<?xml version="1.0"?> <!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd"> <rss version="0.91"> <channel> <title>null</title> <description>Useful Java code examples</description> <link>http://www.rgagnon.com/howto.html</link> <language>en</language> <copyright>(c)Real Gagnon 2007</copyright> <pubDate>29 mai 2007 23:48:42 -0400</pubDate> <item> <title>Real's HowTo</title> <link>http://www.rgagnon.com/java-details/</link> <description>Cool java snippet!</description> </item> </channel> </rss>
Creating a feed with Rome
ROME stands for RSS and Atom Utilities.ROME includes a set of parsers and generators for the various flavors of syndication feeds, as well as converters to convert from one format to another. The project is hosted by java.net at http://wiki.java.net/bin/view/Javawsxml/Rome.
ROME requires JDOM 1.0 (http://www.jdom.org/).
import com.sun.syndication.feed.synd.*; import com.sun.syndication.io.SyndFeedOutput; import java.io.FileWriter; import java.io.Writer; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; public class CreateRSSFeedUsingRome { private static final DateFormat DATE_PARSER = new SimpleDateFormat("yyyy-MM-dd"); public static void main(String[] args) { try { String feedType = "rss_2.0"; String fileName = "feed.xml"; SyndFeed feed = new SyndFeedImpl(); feed.setFeedType(feedType); feed.setTitle("Real's HowTo"); feed.setLink("http://www.rgagnon.com/howto.html"); feed.setDescription("Useful Java code examples"); List entries = new ArrayList(); SyndEntry entry; SyndContent description; entry = new SyndEntryImpl(); entry.setTitle("Real's HowTo"); entry.setLink("http://www.rgagnon.com/java-details/"); entry.setPublishedDate(DATE_PARSER.parse("2004-06-08")); description = new SyndContentImpl(); description.setType("text/plain"); description.setValue("Cool java snippet!"); entry.setDescription(description); entries.add(entry); feed.setEntries(entries); Writer writer = new FileWriter(fileName); SyndFeedOutput output = new SyndFeedOutput(); output.output(feed,writer); writer.close(); } catch (Exception ex) { ex.printStackTrace(); } } }
<?xml version="1.0" encoding="UTF-8"?> <rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"> <channel> <title>Real's HowTo</title> <link>http://www.rgagnon.com/howto.html</link> <description>Useful Java code examples</description> <item> <title>Real's HowTo</title> <link>http://www.rgagnon.com/java-details/</link> <description>Cool java snippet!</description> <pubDate>Tue, 08 Jun 2004 04:00:00 GMT</pubDate> <guid>http://www.rgagnon.com/java-details/</guid> <dc:date>2004-06-08T04:00:00Z</dc:date> </item> </channel> </rss>