Creating a RSS Feed using ROME

Creating a RSS Feed using Java is straightforward. You just need to know how to use ROME API. The website presents some sample codes too. Following code, copied from Codeidol.com, will be useful.



import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.util.List;

import com.sun.syndication.feed.WireFeed;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.feed.synd.SyndPerson;
import com.sun.syndication.feed.synd.SyndPersonImpl;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.WireFeedOutput;

public class ExampleRomeOutput {

public static void main(String[] args) {
if (args.length != 1) {
System.err
.println("Usage: java javaxml3.ExampleRomeOutput [feedtype]");
return;
}

SyndPerson author = new SyndPersonImpl( );
author.setEmail("editor@example.org");
author.setName("Example Person");

SyndFeed feed = new SyndFeedImpl( );
feed.setTitle("Example Feed Output from ROME");
feed.setDescription("The Example Organization web site");
feed.setAuthors(Collections.singletonList(author));
feed.setLink("http://www.example.org/");
feed.setPublishedDate(new Date( ));

SyndEntry entry = new SyndEntryImpl( );
entry.setTitle("First Entry Title");
entry.setLink("http://www.example.org/item1");
SyndContent description = new SyndContentImpl( );
description.setValue("News about the Example project");
description.setType("text");
entry.setDescription(description);
entry.setAuthors(Collections.singletonList(author));
feed.getEntries( ).add(entry);

entry = new SyndEntryImpl( );
entry.setTitle("Second Entry Title");
entry.setLink("http://www.example.org/item2");
description = new SyndContentImpl( );
description.setValue("More news about the Example project");
description.setType("html");
entry.setDescription(description);
entry.setAuthors(Collections.singletonList(author));
feed.getEntries( ).add(entry);

List supportedTypes = feed.getSupportedFeedTypes( );
String feedType = args[0];
if (!supportedTypes.contains(feedType)) {
System.err.println("Feed Type is not supported.");
System.err.println("Supported feed types are: "
+ supportedTypes.toString( ));
return;
}

WireFeed rssFeed = feed.createWireFeed(feedType);

WireFeedOutput output = new WireFeedOutput( );
try {
output.output(rssFeed, new OutputStreamWriter(System.out));
} catch (IOException e) {
System.err.println("Unable to output feed: " + e.getMessage( ));
e.printStackTrace( );
} catch (FeedException e) {
System.err.println("Unable to generate feed: " + e.getMessage( ));
e.printStackTrace( );
}
}

}

0 comments:

Post a Comment