Tuesday, August 14, 2007

Implementing an RSS reader using Java



RssParser rssReader = RssParserFactory.createDefault();

PrintStream printStream = System.out;
System.setOut( new PrintStream( new ByteArrayOutputStream() ) );
Rss rss = rssReader
.parse( new URL(
"http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml" ) );
System.setOut( printStream );

Collection items = rss.getChannel().getItems();
if( items != null && !items.isEmpty() )
{
// Iterate over our main elements. Should have one for each article
for( Iterator i = items.iterator() ; i.hasNext() ; System.out.println() )
{
Item item = (Item)i.next();
System.out.println( "Title: " + item.getTitle() );
System.out.println( "Link: " + item.getLink() );
System.out.println( "Description: " + item.getDescription() );
}

}

// Iterate over categories if we are provided with any
Collection categories = rss.getChannel().getCategories();
if( categories != null && !categories.isEmpty() )
{
Category cat;
for( Iterator i = categories.iterator() ; i.hasNext() ; System.out
.println( "Category Domain: " + cat.getDomain() ) )
{
cat = (Category)i.next();
System.out.println( "Category: " + cat );
}

}


Get the full project

No comments: