In our grails application, we recently met with a requirement where we had to facilitate the application user to post his blog post url in our application. The application then had to fetch the blog post’s content and the fetched content had to be displayed in our application. The other part of this requirement was that we had to use the blog’s feed url to fetch the content.
Aware of the fact that the feed’s structure would vary with the varying feed formats, and that we were a little too short of time, the manual processing of all formats was out of question. A little research on the requirement, our good ‘ol friend Google helped us reach the doorstep of Rome Tools.
The sign on the board read “All Feeds Lead To Rome”.
Stepping inside we found that we had hit the jackpot. Upon further research on how Rome works this is what we found.
Add the following jar dependency in your BuilConfig.groovy
ROME, includes a set of parsers and generators for the various syndication feeds formats and the converters to convert from one format to another. The parsers can then give you back Java objects that are of a generic normalized SyndFeed class.
This lets you work on with the data without bothering about the incoming or outgoing feed type. All you need is a feed url and the following three lines of code.
[groovy]URL feedUrl = new URL(<rss feed url to parse>;);
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedUrl));[/groovy]
Pretty neat isn’t it.!!
Well, now here is a brief about what the above code does that makes your life easier.
I. Creating a SyndFeedInput instance
[groovy]SyndFeedInput input = new SyndFeedInput();[/groovy]
The above line of code calls SyndFeedInput to parse the feed. It creates a SyndFeedInput instance that will work with any syndication feed type (RSS and Atom versions)
II. Reading the syndication feed and extracting the parsed feed
[groovy]SyndFeed feed = input.build(new XmlReader(feedUrl));[/groovy]
The above line of code instructs the SyndFeedInput to read the syndication feed from the character based input stream of the URL pointing to the feed.
Following is a brief about how ROME makes this happen:
1. The SyndFeedInput.build() method returns a SyndFeed instance that can be easily processed.
2. SyndFeedInput delegates to WireFeedInput to do the actual parsing. The WireFeedInput uses a PluginManager of class FeedParsers to pick the right parser that then parses the feed. 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.
3. The parser then, using JDom, parses the feed into a WireFeed. The returned WireFeed is an instance of class:
i) Channel, if the feed is in an RSS format.
ii) Feed, if the feed is in Atom format.
4. SyndFeedInput then uses the returned WireFeedInput to create a SyndFeedImpl that implements SyndFeed (an interface that represents a format independent feed).
5. SyndFeedImpl then with the SyndFeedInput.build() command converts the format specific WireFeed to a format-independent SyndFeed instance.
6. The SyndFeedInput then returns to you the SyndFeed containing the parsed feed.
RSS 0.90, RSS 0.91 Netscape, RSS 0.91 Userland, RSS 0.92, RSS 0.93, RSS 0.94, RSS 1.0, RSS 2.0, Atom 0.3, Atom 1.0
For furthur understanding of ROME, refer to the following link for documentation: