Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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" />
</entry>

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">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.giantflyingsaucer</groupId>
  <artifactId>ParseATOM</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>ParseATOM</name>
  <url>http://maven.apache.org</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.abdera</groupId>
      <artifactId>abdera-parser</artifactId>
      <version>1.1.2</version>
    </dependency>
  </dependencies>
</project>

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
Link: http://127.0.0.1/namespace/feed/entries/urn:uuid:0fb3e0ec-ea46-4f13-9dc3-20df998e9d88
Category: Python
Category: Java

 

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

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}