Reading an ATOM feed using Rome and Java
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" /> </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: 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.giantflyingsaucer</groupId>
<artifactId>ParseATOMRome</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ParseATOMRome</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>net.java.dev.rome</groupId>
<artifactId>rome</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
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 Link: http://127.0.0.1/namespace/feed/entries/urn:uuid:0fb3e0ec-ea46-4f13-9dc3-20df998e9d88 Content: Content for entry 1 Category: Python Category: Java
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





