Creating an RSS Feed (part 2)Tag(s): XML
To create a RSS feed using external librairies, see this HowTo
RSS Feed are XML files. They are easy to create, you can do it yourself.
First we need a bean representing the header
package com.rgagnon.howto.rss;
public class RSSHeader {
private String title = "";
private String description = "";
private String link = "";
private String language = "";
private String copyright = "";
private String author = "";
private String guid = "";
private String pubdate ="";
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 getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getCopyright() {
return copyright;
}
public void setCopyright(String copyright) {
this.copyright = copyright;
}
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;
}
public String getPubDate() {
return pubdate;
}
public void setPubDate(String pubdate) {
this.pubdate = pubdate;
}
}
Next we need a bean representing an entry
package com.rgagnon.howto.rss;
public class RSSEntry {
private String title = "";
private String link = "";
private String description = "";
private String language = "";
private String copyright = "";
private String pubDate = "";
private String guid = "";
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getCopyright() {
return copyright;
}
public void setCopyright(String copyright) {
this.copyright = copyright;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
}
Then a class to contain the header plus the entries
package com.rgagnon.howto.rss;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
public class RSSFeed {
private RSSHeader header;
private List<RSSEntry> entries;
public void setHeader(RSSHeader header){
this.header = header;
}
public void setEntries(List entries){
this.entries = entries;
}
public RSSHeader getHeader() {
return header;
}
public List<RSSEntry> getEntries() {
return entries;
}
public static String formatDate(Calendar cal) {
SimpleDateFormat sdf = new SimpleDateFormat(
"EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
return sdf.format(cal.getTime());
}
}
A class to build the XML
package com.rgagnon.howto.rss;
import java.io.FileOutputStream;
import java.util.Iterator;
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;
public class RSSWriter {
private static String XML_BLOCK = "\n";
private static String XML_INDENT = "\t";
public static void write(RSSFeed rssfeed, String xmlfile) throws Exception {
XMLOutputFactory output = XMLOutputFactory.newInstance();
XMLEventWriter writer = output.createXMLEventWriter
(new FileOutputStream(xmlfile));
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
XMLEvent endSection = eventFactory.createDTD(XML_BLOCK);
StartDocument startDocument = eventFactory.createStartDocument();
writer.add(startDocument);
writer.add(endSection);
StartElement rssStart = eventFactory.createStartElement("", "", "rss");
writer.add(rssStart);
writer.add(eventFactory.createAttribute("version", "2.0"));
writer.add(endSection);
writer.add(eventFactory.createStartElement("", "", "channel"));
writer.add(endSection);
RSSHeader header = rssfeed.getHeader();
createNode(writer, "title", header.getTitle());
createNode(writer, "link", header.getLink());
createNode(writer, "description", header.getDescription());
createNode(writer, "language", header.getLanguage());
createNode(writer, "copyright", header.getCopyright());
createNode(writer, "pubDate", header.getPubDate());
Iterator<RSSEntry> iterator = rssfeed.getEntries().iterator();
while (iterator.hasNext()) {
RSSEntry entry = iterator.next();
writer.add(eventFactory.createStartElement("", "", "item"));
writer.add(endSection);
createNode(writer, "title", entry.getTitle());
createNode(writer, "description", entry.getDescription());
createNode(writer, "link", entry.getLink());
createNode(writer, "guid", entry.getGuid());
createNode(writer, "pubDate", entry.getPubDate());
writer.add(eventFactory.createEndElement("", "", "item"));
writer.add(endSection);
}
writer.add(endSection);
writer.add(eventFactory.createEndElement("", "", "channel"));
writer.add(endSection);
writer.add(eventFactory.createEndElement("", "", "rss"));
writer.add(endSection);
writer.add(eventFactory.createEndDocument());
writer.close();
}
private static void createNode
(XMLEventWriter eventWriter, String name, String value)
throws XMLStreamException {
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
XMLEvent endSection = eventFactory.createDTD(XML_BLOCK);
XMLEvent tabSection = eventFactory.createDTD(XML_INDENT);
StartElement sElement = eventFactory.createStartElement("", "", name);
eventWriter.add(tabSection);
eventWriter.add(sElement);
Characters characters = eventFactory.createCharacters(value);
eventWriter.add(characters);
EndElement eElement = eventFactory.createEndElement("", "", name);
eventWriter.add(eElement);
eventWriter.add(endSection);
}
}
Finally, an example on how to generate a file
package com.rgagnon.howto.rss;
import java.util.ArrayList;
import java.util.Calendar;
public class TestRSSWriter {
private static String RSSFEED = "c:/temp/feed.xml";
public static void main(String[] args) {
System.out.println("Creation RSS Feed (" + RSSFEED + ")");
RSSFeed feed = new RSSFeed();
RSSHeader header = new RSSHeader();
header.setCopyright("Copyright by Real Gagnon");
header.setTitle("Real's HowTo");
header.setDescription("Useful code snippets for Java");
header.setLanguage("en");
header.setLink("http://www.rgagnon.com");
header.setPubDate(RSSFeed.formatDate(Calendar.getInstance()));
feed.setHeader(header);
ArrayList<RSSEntry> entries = new ArrayList<RSSEntry>();
RSSEntry entry = new RSSEntry();
entry.setTitle("The PDF are updated");
entry.setDescription("Java (756 pages), Powerbuilder (197), Javascript (99) and VBS (32)");
entry.setGuid("http://64.18.163.122/rgagnon/download/index.htm");
entry.setLink("http://64.18.163.122/rgagnon/download/index.htm");
entry.setPubDate(RSSFeed.formatDate(Calendar.getInstance()));
entries.add(entry);
entry = new RSSEntry();
entry.setTitle("Java : Display a TIF");
entry.setDescription("Using JAI, how to display a TIF file");
entry.setGuid("http://www.rgagnon.com/javadetails/java-0605.html");
entry.setLink("http://www.rgagnon.com/javadetails/java-0605.html");
entry.setPubDate(RSSFeed.formatDate(Calendar.getInstance()));
entries.add(entry);
feed.setEntries(entries);
try {
RSSWriter.write(feed, TestRSSWriter.RSSFEED);
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("Done.");
}
}
The result is
<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>Real's HowTo</title>
<link>http://www.rgagnon.com</link>
<description>Useful code snippets for Java</description>
<language>en</language>
<copyright>Copyright by Real Gagnon</copyright>
<pubDate>Mon, 23 Jun 2008 11:15:31 -0400</pubDate>
<item>
<title>The PDF are updated</title>
<description>Java (756 pages), Powerbuilder (197), Javascript (99) and VBS (32)</description>
<link>http://64.18.163.122/rgagnon/download/index.htm</link>
<guid>http://64.18.163.122/rgagnon/download/index.htm</guid>
<pubDate>Mon, 23 Jun 2008 11:15:31 -0400</pubDate>
</item>
<item>
<title>Java : Display a TIF</title>
<description>Using JAI, how to display a TIF file</description>
<link>http://www.rgagnon.com/javadetails/java-0605.html</link>
<guid>http://www.rgagnon.com/javadetails/java-0605.html</guid>
<pubDate>Mon, 23 Jun 2008 11:15:31 -0400</pubDate>
</item>
</channel>
</rss>
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com