Reading an ATOM feed using Apache Abdera and Java

DZone 's Guide to

Reading an ATOM feed using Apache Abdera and Java

· Java Zone ·
Free Resource

Today I’ll show you how to read an ATOM feed using Apache Abdera 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 the: abdera-parser. 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 java.net.URL;
import java.util.List;
import org.apache.abdera.Abdera;
import org.apache.abdera.model.Document;
import org.apache.abdera.model.Entry;
import org.apache.abdera.model.Feed;
import org.apache.abdera.model.Category;
import org.apache.abdera.model.Link;
import org.apache.abdera.parser.Parser;
public class App {
    private static Abdera abdera = null;
    public static synchronized Abdera getInstance() {
        if (abdera == null) {
            abdera = new Abdera();
        return abdera;
    public static void main(String[] args) {
        Parser parser = getInstance().getParser();
        try {
            URL url = new URL("http://localhost:8080/namespace/feed");
            Document<Feed> doc = parser.parse(url.openStream(), url.toString());
            Feed feed = doc.getRoot();
            // Get the feed title
            System.out.println("Feed Title: " + feed.getTitle());
            // Get the entry items...
            for (Entry entry : feed.getEntries()) {
                System.out.println("Title: " + entry.getTitle());
                System.out.println("Unique Identifier: " + entry.getId().toString());
                System.out.println("Updated Date: " + entry.getUpdated().toString());
                System.out.println("Published Date: " + entry.getPublished());
                System.out.println("Content: " + entry.getContent());
                // Get the links
                for (Link link : (List<Link>) entry.getLinks()) {
                    System.out.println("Link: " + link.getHref());
                // Get the categories
                for (Category category : (List<Category>) entry.getCategories()) {
                    System.out.println("Category: " + category.getTerm());
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());

Run the application and the results should be:

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


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


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}