Over a million developers have joined DZone.

Reading an ATOM feed using Rome and Java

DZone's Guide to

Reading an ATOM feed using Rome and Java

· Java Zone ·
Free Resource

Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.

Today I’ll show you how to read an ATOM feed using Rome with Java. I’m going to be using Atom Hopper as the ATOMPub server and you can check out my previous articles on how to get it running.

With Atom Hopper up and running and assuming the database is clean (no entries), insert the following (very simplified) ATOM XML with Poster (you can find out how to use Poster in this article):

<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom">
  <title>Title 1</title>
  <content>Content for entry 1</content>
  <category term="Python" />
  <category term="Java" />

Note: Make sure if your using Poster to insert this ATOM XML you use an HTTP POST with the content type set to: application/atom+xml

I used Netbeans 7 to create a new Maven Java Application project. You only need to add one dependency to the POM which is: rome. Here is my entire POM.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

The code for the App.java is as follows:

package com.giantflyingsaucer;
import com.sun.syndication.feed.synd.SyndCategoryImpl;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndLinkImpl;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import java.net.URL;
import java.util.List;
public class App {
    public static void main(String[] args) {
        try {
            URL feedUrl = new URL("http://localhost:8080/namespace/feed/");
            SyndFeedInput input = new SyndFeedInput();
            SyndFeed feed = input.build(new XmlReader(feedUrl));
            System.out.println("Feed Title: " + feed.getTitle());
            // Get the entry items...
            for (SyndEntry entry : (List<SyndEntry>) feed.getEntries()) {
                System.out.println("Title: " + entry.getTitle());
                System.out.println("Unique Identifier: " + entry.getUri());
                System.out.println("Updated Date: " + entry.getUpdatedDate());
                // Get the Links
                for (SyndLinkImpl link : (List<SyndLinkImpl>) entry.getLinks()) {
                    System.out.println("Link: " + link.getHref());
                // Get the Contents
                for (SyndContentImpl content : (List<SyndContentImpl>) entry.getContents()) {
                    System.out.println("Content: " + content.getValue());
                // Get the Categories
                for (SyndCategoryImpl category : (List<SyndCategoryImpl>) entry.getCategories()) {
                    System.out.println("Category: " + category.getName());
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());

Run the application and the results should be:

Feed Title: feed
Title: Title 1
Unique Identifier: urn:uuid:0fb3e0ec-ea46-4f13-9dc3-20df998e9d88
Updated Date: Thu Oct 13 18:01:48 CDT 2011
Content: Content for entry 1
Category: Python
Category: Java

From http://www.giantflyingsaucer.com/blog/?p=3313

Connect any Java based application to your SaaS data.  Over 100+ Java-based data source connectors.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}