The new Certification Class of REST With Spring is out:

>> CHECK OUT THE COURSE

1. Overview

RSS (Rich Site Summary or Really Simple Syndication) is a web feed standard that provides readers with an aggregated content from various locations. The user can see what’s been published recently on his favorite blogs, news sites, etc – all in a single place.

Applications can also use RSS to read, manipulate, or publish information through RSS feeds.

This article gives an overview of how to process RSS feeds in Java with the Rome API.

2. Maven Dependencies

We need to add the dependency for Rome API to our project:

<dependency>			
    <groupId>rome</groupId>			
    <artifactId>rome</artifactId>			
    <version>1.0</version>
</dependency>

We can find the latest version on Maven Central.

3. Creating a New RSS Feed

First, let’s create a new RSS feed with the Rome API using the default implementation SyndFeedImpl of the SyndFeed interface. This interface is able to handle all RSS flavors, so we can always feel safe to use it:

SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_1.0");
feed.setTitle("Test title");
feed.setLink("http://www.somelink.com");
feed.setDescription("Basic description");

In this snippet, we’ve created an RSS feed with standard RSS fields such as a title, link, and description. SyndFeed gives the opportunity of adding many more fields, including authors, contributors, copyrights, modules, published dates, images, foreign markups, and languages.

4. Adding an Entry

As we’ve created the RSS feed, now we can add an entry to it. In the example below, we use the default implementation SyndEntryImpl of the SyndEntry interface to create a new entry:

SyndEntry entry = new SyndEntryImpl();
entry.setTitle("Entry title");        
entry.setLink("http://www.somelink.com/entry1");
    
feed.setEntries(Arrays.asList(entry));

5. Adding a Description

As our entry is pretty empty so far, let’s add a description for it. We can do this by using the default implementation SyndContentImpl of the SyndContent interface:

SyndContent description = new SyndContentImpl();
description.setType("text/html");
description.setValue("First entry");

entry.setDescription(description);

With the setType method, we have specified that the content of our description will be a text or HTML.

6. Adding a Category

RSS entries are often classified into categories to simplify the task of finding entries that we are interested in. Let’s see how we can add a category to the entry using the default implementation SyndCategoryImpl of the SyndCategory interface:

List<SyndCategory> categories = new ArrayList<>();
SyndCategory category = new SyndCategoryImpl();
category.setName("Sophisticated category");
categories.add(category);

entry.setCategories(categories);

7. Publishing the Feed

We already have an RSS feed with an entry. Now we want to publish it. For the purpose of this article, by publishing, we mean writing the feed to a stream:

Writer writer = new FileWriter("xyz.txt");
SyndFeedOutput syndFeedOutput = new SyndFeedOutput();
syndFeedOutput.output(feed, writer);
writer.close();

8. Reading an External Feed

We already know how to create a new feed, but sometimes we just need to connect to an existing one.

Let’s see how to read/load a feed, given its URL:

URL feedSource = new URL("http://rssblog.whatisrss.com/feed/");
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedSource));

9. Conclusion

In this article, we have shown how to create an RSS feed with some entries, how to publish the feed, and how to read external feeds.

As always, you can check out the examples provided in this article over on GitHub.

Go deeper into building a REST API with Spring:

>> CHECK OUT THE COURSE