ROME and custom Generator elements

The majority of the articles I’ve seen on using ROME to create Atom and RSS feeds don’t show you how to customize the optional ‘generator’ element that both Atom and RSS support. It’s really easy. I’m assuming that you’ve already created and populated a SyndFeed instance:

SyndFeed feed = ..
WireFeedOutput feedOutput = new WireFeedOutput();
WireFeed wireFeed = feed.createWireFeed();
if (wireFeed.getFeedType().startsWith("atom")) {
  Feed atomFeed = (Feed)wireFeed;
  Generator gen = new Generator();
  gen.setUrl("http://yoursite.com/");
  gen.setValue("Your Site");
  gen.setVersion("1.0");
  atomFeed.setGenerator(gen);
  feedOutput.output(atomFeed, ...);
} else {
  Channel rssFeed = (Channel)wireFeed;
  rssFeed.setGenerator("Your Site 1.0 (http://yoursite.com/)");
  feedOutput.output(rssFeed, ...);
}

Make a decision about which feed you’re going to produce, cast to the appropriate implementation of WireFeed (Channel for RSS and Feed for Atom), and then use the appropriate setters on each of those respective classes.

This entry was posted in J2EE, Syndication, XML. Bookmark the permalink.

2 Responses to ROME and custom Generator elements

  1. wvpv says:

    Thanks for this little write-up. I too found that none of the Rome examples showed how to set the generator and other RSS specific elements.

  2. brianstarke says:

    I’m actually struggling with the reverse problem right now, I’m trying to get the generator value when parsing the feed. The code is pretty much the reverse of your example :


    WireFeedInput input = new WireFeedInput();
    input.setXmlHealerOn(true);

    XmlReader reader = new XmlReader(new URL(feedUrl));
    WireFeed feed = input.build(reader);

    String generator = null;

    if (feed.getFeedType().startsWith("atom")) {
    Feed atomFeed = (Feed) feed;
    atomFeed.getGenerator().getValue();

    generator = atomFeed.getGenerator().getValue() + " " +
    atomFeed.getGenerator().getVersion() + " (" + atomFeed.getGenerator().getUrl() +
    ")";
    } else {
    Channel rssFeed = (Channel) feed;
    generator = rssFeed.getGenerator();
    }

    The Atom generator is parsed correctly, but the RSS one comes back null. Any ideas?

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>