vogella.de

Follow me on twitter
About Lars Vogel
Flattr this

RSS feeds with Java - Tutorial

Lars Vogel

Version 0.7

05.11.2009

Revision History
Revision 0.1-0.203.09.2007Lars Vogel
Created, rss feed creation with Stax
Revision 0.318.07.2009Lars Vogel
Re-structured article
Revision 0.422.07.2009Lars Vogel
change project name, links to XML article
Revision 0.524.07.2009Lars Vogel
used foreach loop
Revision 0.601.11.2009Lars Vogel
small re-write
Revision 0.705.11.2009Lars Vogel
Read RSS feed

RSS Feeds with Java

This article explains how to read and create RSS feeds with Java.

This article uses the Stax XML writer which is part of Java 6.0. Eclipse is used as the Java IDE.


Table of Contents

1. Overview
1.1. RSS - Really Simple Syndication
1.2. RSS with Java
2. Domain model
3. Read RSS Feeds with Stax
3.1. Create Project
3.2. Create the XML Reader
3.3. Test the code
4. Write RSS Feeds with Stax
4.1. Create Project
4.2.
4.3. Test the code
5. Thank you
6. Questions and Discussion
7. Links and Literature

1. Overview

1.1. RSS - Really Simple Syndication

An RSS document is a XML file which can be used to publish blog entries, news and / or audio and video files. The format of the XML file is specified via the RSS specification which is currently in version must be 2.0.

RSS stands for "Really Simple Syndication" (in version 2.0 of the RSS specification).

1.2. RSS with Java

As an RSS feed is a XML file we can use the Java XML parsers to read and create RSS feeds. The following will use the Java Stax XML parser.

For an introduction into XML and its usage with Java see Java XML Tutorial .

2. Domain model

We will use the following domain model for the following examples.

			
package de.vogella.rss.model;

/*
 * Represents one RSS message
 */
public class FeedMessage {

	String title;
	String description;
    String link; 
    String author;
    String guid;
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getLink() {
		return link;
	}
	public void setLink(String link) {
		this.link = link;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getGuid() {
		return guid;
	}
	public void setGuid(String guid) {
		this.guid = guid;
	}
}


		

			
package de.vogella.rss.model;

import java.util.ArrayList;
import java.util.List;

/*
 * Stores an RSS feed
 */
public class Feed {
	
	final String title;
	final String link;
	final String description;
	final String language;
	final String copyright;
	final String pubDate;
	final List<FeedMessage> entries = new ArrayList<FeedMessage>();

	public Feed(String title, String link, String description,
			String language, String copyright, String pubDate) {
				this.title = title;
				this.link = link;
				this.description = description;
				this.language = language;
				this.copyright = copyright;
				this.pubDate = pubDate;
	}

	public List<FeedMessage> getMessages() {
		return entries;
	}

	public String getTitle() {
		return title;
	}

	public String getLink() {
		return link;
	}

	public String getDescription() {
		return description;
	}

	public String getLanguage() {
		return language;
	}

	public String getCopyright() {
		return copyright;
	}


	public String getPubDate() {
		return pubDate;
	}

	@Override
	public String toString() {
		return "Feed [copyright=" + copyright + ", description=" + description
				+ ", language=" + language + ", link=" + link + ", pubDate="
				+ pubDate + ", title=" + title + "]";
	}
	
	

}

		

3. Read RSS Feeds with Stax

3.1. Create Project

Create the Java Project "de.vogella.rss.read". Create the domain model classes in this project.

3.2. Create the XML Reader

Create the following class to read the XML file.

				
package de.vogella.rss.read;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;

import de.vogella.rss.model.Feed;
import de.vogella.rss.model.FeedMessage;

public class RSSFeedParser {
	static final String TITLE = "title";
	static final String DESCRIPTION = "description";
	static final String CHANNEL = "channel";
	static final String LANGUAGE = "language";
	static final String COPYRIGHT = "copyright";
	static final String LINK = "link";
	static final String AUTHOR = "author";
	static final String ITEM = "item";
	static final String PUB_DATE = "pubDate";
	static final String GUID = "guid";

	final URL url;

	public RSSFeedParser(String feedUrl) {
		try {
			this.url = new URL(feedUrl);
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
	}

	@SuppressWarnings("null")
	
	public Feed readFeed() {
		Feed feed=null;
		try {
		
			boolean isFeedHeader = true;
			// Set header values intial to the empty string
			String description="";
			String title="";
			String link="";
			String language="";
			String copyright ="";
			String author = "";
			String pubdate ="";
			String guid ="";

			// First create a new XMLInputFactory
			XMLInputFactory inputFactory = XMLInputFactory.newInstance();
			// Setup a new eventReader
			InputStream in = read();
			XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
			// Read the XML document
			while (eventReader.hasNext()) {

				XMLEvent event = eventReader.nextEvent();

				if (event.isStartElement()) {
					if (event.asStartElement().getName().getLocalPart() == (ITEM)) {
						if (isFeedHeader) {
							isFeedHeader = false;
							feed = new Feed(title, link, description,
									language, copyright, pubdate);
						}
						event = eventReader.nextEvent();
						continue;
					}

					if (event.asStartElement().getName().getLocalPart() == (TITLE)) {
						event = eventReader.nextEvent();
						title = event.asCharacters().getData(); 
						continue;
					}
					if (event.asStartElement().getName().getLocalPart() == (DESCRIPTION)) {
						event = eventReader.nextEvent();
						description= event.asCharacters().getData(); 
						continue;
					}

					if (event.asStartElement().getName().getLocalPart() == (LINK)) {
						event = eventReader.nextEvent();
						link= event.asCharacters().getData(); 
						continue;
					}

					if (event.asStartElement().getName().getLocalPart() == (GUID)) {
						event = eventReader.nextEvent();
						guid= event.asCharacters().getData(); 
						continue;
					}
					if (event.asStartElement().getName().getLocalPart() == (LANGUAGE)) {
						event = eventReader.nextEvent();
						language= event.asCharacters().getData(); 
						continue;
					}
					if (event.asStartElement().getName().getLocalPart() == (AUTHOR)) {
						event = eventReader.nextEvent();
						author= event.asCharacters().getData(); 
						continue;
					}
					if (event.asStartElement().getName().getLocalPart() == (PUB_DATE)) {
						event = eventReader.nextEvent();
						pubdate= event.asCharacters().getData(); 
						continue;
					}
					if (event.asStartElement().getName().getLocalPart() == (COPYRIGHT)) {
						event = eventReader.nextEvent();
						copyright= event.asCharacters().getData(); 
						continue;
					}
				} else if (event.isEndElement()) {
					if (event.asEndElement().getName().getLocalPart() == (ITEM)) {
						FeedMessage message = new FeedMessage();
						message.setAuthor(author);
						message.setDescription(description);
						message.setGuid(guid);
						message.setLink(link);
						message.setTitle(title);
						feed.getMessages().add(message);
						event = eventReader.nextEvent();
						continue;
					}
				}
			}
		} catch (XMLStreamException e) {
			throw new RuntimeException(e);
		}
		return feed;

	}

	private InputStream read() {
		try {
			return url.openStream();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}

			

3.3. Test the code

The following uses a main method to test. You could also use JUnit. See JUnit Tutorial

				
package de.vogella.rss.read;

import de.vogella.rss.model.Feed;
import de.vogella.rss.model.FeedMessage;


public class ReadTest {
public static void main(String[] args) {
	RSSFeedParser parser = new RSSFeedParser("http://www.vogella.de/article.rss");
	Feed feed = parser.readFeed();
	System.out.println(feed);
	for (FeedMessage message : feed.getMessages()) {
		System.out.println(message);
		
	}
	
}
}

			

4. Write RSS Feeds with Stax

4.1. Create Project

Create the Java Project "de.vogella.rss.write". Create the domain model classes in this project.

Create the following class to write the XML file.

				
package de.vogella.rss.write;

import java.io.FileOutputStream;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

import de.vogella.rss.model.Feed;
import de.vogella.rss.model.FeedMessage;

public class RSSFeedWriter {

	private String outputFile;
	private Feed rssfeed;

	public RSSFeedWriter(Feed rssfeed, String outputFile) {
		this.rssfeed = rssfeed;
		this.outputFile = outputFile;
	}

	public void write() throws Exception {

		// Create a XMLOutputFactory
		XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();

		// Create XMLEventWriter
		XMLEventWriter eventWriter = outputFactory
				.createXMLEventWriter(new FileOutputStream(outputFile));

		// Create a EventFactory

		XMLEventFactory eventFactory = XMLEventFactory.newInstance();
		XMLEvent end = eventFactory.createDTD("\n");

		// Create and write Start Tag

		StartDocument startDocument = eventFactory.createStartDocument();

		eventWriter.add(startDocument);

		// Create open tag
		eventWriter.add(end);

		StartElement rssStart = eventFactory.createStartElement("", "", "rss");
		eventWriter.add(rssStart);
		eventWriter.add(eventFactory.createAttribute("version", "2.0"));
		eventWriter.add(end);

		eventWriter.add(eventFactory.createStartElement("", "", "channel"));
		eventWriter.add(end);

		// Write the different nodes

		createNode(eventWriter, "title", rssfeed.getTitle());

		createNode(eventWriter, "link", rssfeed.getLink());

		createNode(eventWriter, "description", rssfeed.getDescription());

		createNode(eventWriter, "language", rssfeed.getLanguage());

		createNode(eventWriter, "copyright", rssfeed.getCopyright());

		createNode(eventWriter, "pubdate", rssfeed.getPubDate());

		for (FeedMessage entry: rssfeed.getMessages()) {
			eventWriter.add(eventFactory.createStartElement("", "", "item"));
			eventWriter.add(end);
			createNode(eventWriter, "title", entry.getTitle());
			createNode(eventWriter, "description", entry.getDescription());
			createNode(eventWriter, "link", entry.getLink());
			createNode(eventWriter, "author", entry.getAuthor());
			createNode(eventWriter, "guid", entry.getGuid());
			eventWriter.add(end);
			eventWriter.add(eventFactory.createEndElement("", "", "item"));
			eventWriter.add(end);

		}

		eventWriter.add(end);
		eventWriter.add(eventFactory.createEndElement("", "", "channel"));
		eventWriter.add(end);
		eventWriter.add(eventFactory.createEndElement("", "", "rss"));

		eventWriter.add(end);

		eventWriter.add(eventFactory.createEndDocument());

		eventWriter.close();
	}

	private void createNode(XMLEventWriter eventWriter, String name,

	String value) throws XMLStreamException {
		XMLEventFactory eventFactory = XMLEventFactory.newInstance();
		XMLEvent end = eventFactory.createDTD("\n");
		XMLEvent tab = eventFactory.createDTD("\t");
		// Create Start node
		StartElement sElement = eventFactory.createStartElement("", "", name);
		eventWriter.add(tab);
		eventWriter.add(sElement);
		// Create Content
		Characters characters = eventFactory.createCharacters(value);
		eventWriter.add(characters);
		// Create End node
		EndElement eElement = eventFactory.createEndElement("", "", name);
		eventWriter.add(eElement);
		eventWriter.add(end);
	}
}

			

4.3. Test the code

The following uses a main method to test. You could also use JUnit. See JUnit Tutorial

Run the program to write the RSS file "articles.rss".

				
package de.vogella.rss.write;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import de.vogella.rss.model.Feed;
import de.vogella.rss.model.FeedMessage;

public class WriteTest {

	public static void main(String[] args) {
		// Create the rss feed
		String copyright = "Copyright hold by Lars Vogel"; 
		String title = "Eclipse and Java Information"; 
		String description = "Eclipse and Java Information";
		String language = "en";
		String link = "http://www.vogella.de"; 
		Calendar cal = new GregorianCalendar();
		Date creationDate = cal.getTime();
		SimpleDateFormat date_format = new SimpleDateFormat(
				"EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z", Locale.US);
		String pubdate = date_format.format(creationDate);
		Feed rssFeeder = new Feed(title, link, description, language, copyright, pubdate);
		

		// Now add one example entry
		FeedMessage feed = new FeedMessage();
		feed.setTitle("RSSFeed");
		feed.setDescription("This is a description");
		feed.setAuthor("nonsense@somewhere.de (Lars Vogel)");
		feed.setGuid("http://www.vogella.de/articles/RSSFeed/article.html");
		feed.setLink("http://www.vogella.de/articles/RSSFeed/article.html");
		rssFeeder.getMessages().add(feed);

		// Now write the file
		RSSFeedWriter writer = new RSSFeedWriter(rssFeeder, "articles.rss");
		try {
			writer.write();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

			

5. Thank you

Thank you for practicing with this tutorial.

I maintain this tutorial in my private time. If you like the information please help me by using flattr or donating or by recommending this tutorial to other people.

Flattr this

6. Questions and Discussion

Before posting questions, please see the vogella FAQ . If you have questions or find an error in this article please use the www.vogella.de Google Group . I have created a short list how to create good questions which might also help you. .

7. Links and Literature

http://feedvalidator.org/ Webpage which validates feeds