I wrote a simple sample program which reads a given RSS feed and display some of the feed entries along with some attributes. It will also display the RSS version, as there are different versions of RSS. http://en.wikipedia.org/wiki/RSS_(file_format)
This simple program is the following:
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndImageImpl;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import com.sun.syndication.feed.synd.SyndContentImpl;
/*
* 2 Required Jar files
* rome-0.9.jar retrieved from http://rome.dev.java.net/
* jdom.jar retrieved from http://www.jdom.org/
*/
public class RSSReader
{
public static void iterateRSSFeed(String rssFeedUrl)
{
try
{
//open up a connection to the rss feed
URLConnection feedUrl = new URL(rssFeedUrl).openConnection();
//Create Feed Object
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedUrl));
System.out.println("Examining rss feed:"+rssFeedUrl+"\n");
System.out.println("Feed type="+feed.getFeedType());
//Iterate through object to get details
List list = feed.getEntries();
System.out.println("Feed image="+feed.getImage());
if (feed.getImage()!=null)
{
SyndImageImpl image = (SyndImageImpl)feed.getImage();
String imageInfo = "Image url:"+image.getUrl()+"\n";
System.out.println(imageInfo);
}
for (int i=0 ; i < list.size(); i++){
//display entry attributes
SyndEntry entry = (SyndEntry)list.get(i);
String display = "Entry:"+i;
display += "\ntitle:"+entry.getTitle();
display += "\nlink:"+entry.getLink();
display += "\nauthor:"+entry.getAuthor();
display += "\npublished:"+entry.getPublishedDate();
display += "\nupdated:"+entry.getUpdatedDate();
display += "\ndescription:"+entry.getDescription();
display += "\ncontent size:"+entry.getContents().size();
if (entry.getContents().size()==1)
{
SyndContentImpl imp = (SyndContentImpl)entry.getContents().get(0);
display += "\ncontent value:"+imp.getValue();
}
display += "\n";
System.out.println(display);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
//James Gosling's blog
String rssFeedUrl = "http://blogs.sun.com/jag/feed/entries/rss";
//Mark Cuban's blog
//rssFeedUrl = "http://www.blogmaverick.com/rss.xml";
//rssFeedUrl = "http://techinitiatives.blogspot.com/feeds/posts/default";
//RSS 2.0
//rssFeedUrl = "http://weather.yahooapis.com/forecastrss?p=FRXX0076&u=c";
iterateRSSFeed(rssFeedUrl);
}
}