XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

ROME in a Day: Parse and Publish Feeds in Java
by Mark Woodman | Pages: 1, 2, 3, 4, 5, 6

Key Ingredients

Before you can jump into coding, you will need to download a few things. Here is the list of ingredients you will need for today's recipe:

Now that you have downloaded the above resources and have your development environment set up, let's get into the code.

Creating the FeedWarmer

ROME provides a series of bean interfaces which can be used to access the data of a syndicated feed, regardless of format. As we write the FeedWarmer class, we'll make use of the SyndFeed and SyndEntry interfaces to keep our code as clean as possible. To understand the broader scope of the classes we use, be sure to keep the ROME Javadocs close by.

Let's start with the basic structure of the FeedWarmer class. We'll build on this structure as we go through the tutorial, but you can also use the complete FeedWarmer.java source code as a reference.

public class FeedWarmer
{
    public FeedWarmer() 
    {}

    public String warmFeed(URL url, String outFormat)
            throws IOException, FeedException 
    {}
    
    private void addFooter(SyndEntry entry)
    {}

    private String createFooter(String original, String link,
                                String title)
    {}

    public static void main(String[] args) throws Exception
    {}
}

As shown above, the structure of our program is fairly simple. We'll fill out the details as we go, following this basic approach:

  1. We will create a default constructor, FeedWarmer(), to initialize any instance variables.
  2. The primary method used in this class is warmFeed(), which takes an RSS or Atom feed URL and a desired output format. It is the control point to parse a feed into a SyndFeed bean, modify the SyndEntry beans representing each feed item, and return the results in a new format.
  3. The addFooter() method will be used to add an interactive footer to each SyndEntry bean. These beans hold the data from a syndicated feed, and represent either an RSS item or an Atom entry.
  4. createFooter() is a utility method which uses the arguments passed in to create a footer. It appends the footer to the original feed HTML and returns the result.
  5. Last but not least, we'll use a standard main() method to try out our work.

Instance Variables and Constructor

Let's begin by setting up some instance variables and the constructor. The ROME library has utility classes for parsing and publishing syndicated feeds, so our FeedWarmer will need an instance of SyndFeedInput and SyndFeedOutput. We will also be using a ROME module to handle <content:encoded /> XML elements. Modules are invoked using their target schema, so we'll also add a URI for purl.org's content schema:

public class FeedWarmer
{
    /** Namespace URI for content:encoded elements */
    private static String CONTENT_NS =
            "http://purl.org/rss/1.0/modules/content/";

    /** Parses RSS or Atom to instantiate a SyndFeed. */
    private SyndFeedInput input;

    /** Transforms SyndFeed to RSS or Atom XML. */
    private SyndFeedOutput output;
    
    /**
     * Default constructor.
     */
    public FeedWarmer()
    {
        input = new SyndFeedInput();
        output = new SyndFeedOutput();
    }

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow