Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to parse ATOM/RSS feed using Java?

In this post, you are going to learn below things.
a.   What is ATOM publishing protocol?
b.   ATOM vs RSS
c.   Different libraries that support ATOM feed.
d.   Example of atom feed
e.   Parse ATOM feed using ROME library

What is Atom Publishing Protocol?
It is application-level protocol for publishing and editing Web resources. The Atom Publishing Protocol is documented in rfc5023. The schema/format of Atom is documented in rfc4287.

ATOM Vs RSS
Atom are the two main standards of web syndication. ATOM protocol was developed to overcome the flaws in RSS feed format. A RSS file   can have extension of .rss or .xml, where as an atom file can have extensions of .atom or .xml

Different libraries that support ATOM/RSS feed
Below libraries can be used to parse ATOM feed.
a.   Apache Abdera
b.   ROME

If you would like to know, detailed information about Abdera library, I will suggest you, go through my post.

Example of ATOM feed
A typical example of ATOM feed looks like below.


xmlns="http://www.w3.org/2005/Atom">
tag:blogger.com,1999:blog-3062500619105519975
2016-12-17T20:29:02.425Z
<span style="color: #0000cc;">type=</span><span style="background-color: #fff0f0;">"text"</span><span style="color: #007700;">></span>java tutorial : Blog to learn java programming<span style="color: #007700;"></span>
type="text">Learners Blog
term="Programming" scheme="scheme" label="Java Tutorial for beginners"/>

Krishna
[email protected]
http://self-learning-java-tutorial.blogspot.com

uri="http://www.blogger.com" version="7.00">Blogger
http://1.bp.blogspot.com/-0dfQR8-klyU/U23yFAdHRyI/AAAAAAAAAeU/jITsK3kz3k4/s1600/1.bmp
href="http://self-learning-java-tutorial.blogspot.com/feeds/posts/default" rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" title="" hreflang="" length="0"/>
href="http://www.blogger.com/feeds/3062500619105519975/posts/default?alt=atom" rel="self" type="application/atom+xml" title="" hreflang="" length="0"/>

hari krishna
[email protected]
http://self-learning-java-tutorial.blogspot.com

http://1.bp.blogspot.com/-0dfQR8-klyU/U23yFAdHRyI/AAAAAAAAAeU/jITsK3kz3k4/s1600/1.bmp
type="text">Copyrighted : Java Tutorial

tag:blogger.com,1999:blog-3062500619105519975.post-918659526208416960
2016-12-17T20:29:02.445Z
2016-12-17T20:29:02.445Z
<span style="color: #0000cc;">type=</span><span style="background-color: #fff0f0;">"text"</span><span style="color: #007700;">></span>SWT: Slider Tutorial<span style="color: #007700;"></span>
type="html">Slider class is used to define Slider widget

Hari Krishna Gurram
[email protected]
http://self-learning-java-tutorial.blogspot.com


Rama Krishna Gurram
[email protected]
http://self-learning-java-tutorial.blogspot.com


Ritweek Mehenty
[email protected]
http://self-learning-java-tutorial.blogspot.com


Sailaja Navakotla
[email protected]
http://self-learning-java-tutorial.blogspot.com

term="GUI" scheme="scheme" label="SWT for beginners"/>
type="text">Copyrighted : SWT Tutorial
type="text">SWT For beginners



Parse ATOM feed using ROME library
I am going to use below maven dependency to parse an atom feed.

    com.rometools
    rome
    1.9.0

ROME is a set of Atom/RSS Java utilities that make it easy to work in Java with most syndication formats.

At the time of writing this article, ROME supports below RSS and ATOM formats.

RSS 0.90,
RSS 0.91,
RSS 0.92,
RSS 0.93,
RSS 0.94,
RSS1.0,
RSS 2.0,
ATOM 0.3 feed and
ATOM 1.0 feed

Below step-by-step procedure explains how to parse and get the entries from atom feed.

Step 1: Create an URL instance from the atom feed.

String atomFeedURL = "https://self-learning-java-tutorial.blogspot.in/atom.xml";
URL feedUrl = new URL(atomFeedURL);

Step 2: Get Syndicate feed from the feed url

SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedUrl));

Step 3: Get all the entries from syndicate feed, print the author names and title.

List entries = feed.getEntries();

for (SyndEntry syndEntry : entries) {
         String author = syndEntry.getAuthor();
         String title = syndEntry.getTitle();
         System.out.println("author : " + author);
         System.out.println("Title : " + title);
}

Find the below working application.


AtomParserDemo.java
package com.sample.util;

import java.net.URL;
import java.util.List;

import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.XmlReader;

public class AtomParserDemo {

private static String atomFeedURL = "https://self-learning-java-tutorial.blogspot.in/atom.xml";

public static void main(String args[]) throws Exception {
URL feedUrl = new URL(atomFeedURL);

SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedUrl));

ListSyndEntry> entries = feed.getEntries();

for (SyndEntry syndEntry : entries) {
String author = syndEntry.getAuthor();
String title = syndEntry.getTitle();
System.out.println("author : " + author);
System.out.println("Title : " + title);
}

}
}


Sample Output
author : hari krishna
Title : How to chain multiple different InputStreams into one InputStream
author : hari krishna
Title : hamcrest: hasItemInArray: Check the existence of item in the array
author : hari krishna
Title : Base64 encoding and decoding in Java
author : hari krishna
Title : Hamcrest: array: Match every element of array with specific matcher
author : hari krishna
Title : How to write multiple input streams to a file
author : hari krishna
Title : Hamcrest: Matchers to work with arrays
author : hari krishna
Title : Hamcrest: stringContainsInOrder: Matches a string against all the substrings
author : hari krishna
Title : HamCrest: isEmptyOrNullString: Match if the string is empty or null
author : hari krishna
Title : Hamcrest: isEmptyString: Match if the string is empty
author : hari krishna
Title : Hamcrest: equalToIgnoringWhiteSpace: check equality of strings by ignoring case and white spaces
author : hari krishna
Title : Hamcrest: equalToIgnoringCase: check equality of strings by ignoring case
author : hari krishna
Title : Hamcrest: endsWith: Check whether string ends with given string
author : hari krishna
Title : Hamcrest: startsWith: Check whether string starts with given string
author : hari krishna
Title : Hamcrest: containsString: match sub string
author : hari krishna
Title : Hamcrest: Matchers to work with strings
author : hari krishna
Title : Hamcrest: notNullValue : Creates a matcher that matches if examined object is not null
author : hari krishna
Title : Hamcrest: nullValue: Creates a matcher that matches if examined object is null
author : hari krishna
Title : Hamcrest: Match null values
author : hari krishna
Title : Hamcrest: Hello World Application
author : hari krishna
Title : Introduction to Hamcrest
author : hari krishna
Title : Hamcrest tutorial
author : hari krishna
Title : Kotlin: complementary variance annotation: in
author : hari krishna
Title : Kotlin: Generics
author : hari krishna
Title : Kotlin: Data classes: Destructuring declarations


Refernece
https://rometools.github.io/rome/

You may like
Miscellaneous
Interview Questions
Progase64 encoding and decoding in Java
How to chain multiple different InputStreams into one InputStream
Get Enumeration from Java List
A short tutorial on FindBugs
Create XML sitemaps in Java







This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

How to parse ATOM/RSS feed using Java?

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×