258 lines
No EOL
13 KiB
HTML
258 lines
No EOL
13 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<!-- Generated by Apache Maven Doxia Site Renderer 1.4 at 2013-09-27 -->
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
|
<title>ROME - Rss and atOM utilitiEs (ROME) v0.5 Tutorial, Using ROME to create and write a syndication feed</title>
|
|
<style type="text/css" media="all">
|
|
@import url("../css/maven-base.css");
|
|
@import url("../css/maven-theme.css");
|
|
@import url("../css/site.css");
|
|
</style>
|
|
<link rel="stylesheet" href="../css/print.css" type="text/css" media="print" />
|
|
<meta name="author" content="mkurz" />
|
|
<meta name="Date-Creation-yyyymmdd" content="20110815" />
|
|
<meta name="Date-Revision-yyyymmdd" content="20130927" />
|
|
<meta http-equiv="Content-Language" content="en" />
|
|
|
|
</head>
|
|
<body class="composite">
|
|
<div id="banner">
|
|
<a href="http://github.com/rometools/" id="bannerLeft">
|
|
<img src="../images/romelogo.png" alt="ROME" />
|
|
</a>
|
|
<div class="clear">
|
|
<hr/>
|
|
</div>
|
|
</div>
|
|
<div id="breadcrumbs">
|
|
|
|
|
|
<div class="xright">
|
|
|
|
<span id="publishDate">Last Published: 2013-09-27</span>
|
|
| <span id="projectVersion">Version: 1.1-SNAPSHOT</span>
|
|
</div>
|
|
<div class="clear">
|
|
<hr/>
|
|
</div>
|
|
</div>
|
|
<div id="leftColumn">
|
|
<div id="navcolumn">
|
|
|
|
|
|
<h5>Rome</h5>
|
|
<ul>
|
|
<li class="none">
|
|
<a href="../index.html" title="Overview">Overview</a>
|
|
</li>
|
|
<li class="collapsed">
|
|
<a href="../HowRomeWorks/index.html" title="How Rome Works">How Rome Works</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="../RssAndAtOMUtilitiEsROMEV0.5AndAboveTutorialsAndArticles/index.html" title="Tutorials And Articles">Tutorials And Articles</a>
|
|
</li>
|
|
<li class="collapsed">
|
|
<a href="../ROMEReleases/index.html" title="Releases">Releases</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="../ROMEDevelopmentProposals/index.html" title="ROME Development Proposals">ROME Development Proposals</a>
|
|
</li>
|
|
</ul>
|
|
<h5>Project Documentation</h5>
|
|
<ul>
|
|
<li class="collapsed">
|
|
<a href="../project-info.html" title="Project Information">Project Information</a>
|
|
</li>
|
|
<li class="collapsed">
|
|
<a href="../project-reports.html" title="Project Reports">Project Reports</a>
|
|
</li>
|
|
</ul>
|
|
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
|
|
<img class="poweredBy" alt="Built by Maven" src="../images/logos/maven-feather.png" />
|
|
</a>
|
|
|
|
|
|
</div>
|
|
</div>
|
|
<div id="bodyColumn">
|
|
<div id="contentBox">
|
|
<div class="section">
|
|
<h2>Rss and atOM utilitiEs (ROME) v0.5 Tutorial, Using ROME to create and write a syndication feed<a name="Rss_and_atOM_utilitiEs_ROME_v0.5_Tutorial_Using_ROME_to_create_and_write_a_syndication_feed"></a></h2>
|
|
<p><b>Software requirements:</b> J2SE 1.4+, JDOM 1.0 and ROME 0.5.</p>
|
|
<p>ROME represents syndication feeds (RSS and Atom) as instances of the com.sun.syndication.synd.SyndFeed interface. The SyndFeed interfaces and its properties follow the Java Bean patterns. The default implementations provided with ROME are all lightweight classes.</p>
|
|
<p>Creating a feed with SyndFeed beans consists of creating beans and setting their properties. The following code fragments show how a SyndFeed bean with 3 entries is created.</p>
|
|
<p>First the SyndFeed instance is created, the preferred syndication format is set and the feed header info (title, link, description) is also set.</p>
|
|
<div class="source">
|
|
<pre>
|
|
SyndFeed feed = new SyndFeedImpl();
|
|
feed.setFeedType(feedType);
|
|
|
|
feed.setTitle("Sample Feed (created with ROME)");
|
|
feed.setLink("http://rome.dev.java.net");
|
|
feed.setDescription("This feed has been created using ROME (Java syndication utilities");
|
|
</pre></div>
|
|
<p>Then a list for entries is created, entries are created and added to the list. Each entry is set with a title, link, published date and a description. The description for the first entry is plain test, for the third entry is HTML. After each entry is created is added to the list.</p>
|
|
<div class="source">
|
|
<pre>
|
|
List entries = new ArrayList();
|
|
SyndEntry entry;
|
|
SyndContent description;
|
|
|
|
entry = new SyndEntryImpl();
|
|
entry.setTitle("ROME v1.0");
|
|
entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome01");
|
|
entry.setPublishedDate(DATE_PARSER.parse("2004-06-08"));
|
|
description = new SyndContentImpl();
|
|
description.setType("text/plain");
|
|
description.setValue("Initial release of ROME");
|
|
entry.setDescription(description);
|
|
entries.add(entry);
|
|
[...]
|
|
entry = new SyndEntryImpl();
|
|
entry.setTitle("ROME v3.0");
|
|
entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome03");
|
|
entry.setPublishedDate(DATE_PARSER.parse("2004-07-27"));
|
|
description = new SyndContentImpl();
|
|
description.setType("text/html");
|
|
description.setValue("<p>More Bug fixes, mor API changes, some new features and some Unit testing</p>"+
|
|
"<p>For details check the <a href=\"https://rometools.jira.com/wiki/display/ROME/Change+Log#ChangeLog-Changesmadefromv0.3tov0.4\">Changes Log</a></p>");
|
|
entry.setDescription(description);
|
|
entries.add(entry);
|
|
</pre></div>
|
|
<p>Finally the list with entries is added to the SyndFeed bean.</p>
|
|
<div class="source">
|
|
<pre>
|
|
feed.setEntries(entries);
|
|
</pre></div>
|
|
<p>The SyndFeed bean is now ready to be written out to a syndication feed XML document. Note that any of supported syndication formats can be set in the feedType property.</p>
|
|
<p>ROME includes generators that allow producing syndication feed XML documents from SyndFeed instances. The SyndFeedOutput class handles the generation of the syndication feed XML documents on any of the supported feed formats (RSS and Atom). The developer does not need to worry about selecting the right generator for a syndication feed, the SyndFeedOutput will take care of it by looking at the information in the SyndFeed bean. All it takes to write a syndication feed XML document using ROME -assuming you have a SyndFeed bean and a Writer instance- are the following lines of code:</p>
|
|
<div class="source">
|
|
<pre>
|
|
SyndFeed feed = ...;
|
|
Writer writer = ...;
|
|
|
|
SyndFeedOutput output = new SyndFeedOutput();
|
|
output.output(feed,writer);
|
|
</pre></div>
|
|
<p>First a SyndFeedOutput instance is created, this instance will work with any syndication feed type (RSS and Atom versions). Then the feed and the writer are given to the SyndFeedOutput instance, the SyndFeedOutput will write the syndication feed XML document represented by the SyndFeed bean to the Writer stream.</p>
|
|
<p>Following is the full code for a Java application that creates a syndication feed and writes it to a file in the specified syndication format.</p>
|
|
<div class="source">
|
|
<pre>
|
|
package com.sun.syndication.samples;
|
|
|
|
import com.sun.syndication.feed.synd.*;
|
|
import com.sun.syndication.io.SyndFeedOutput;
|
|
|
|
import java.io.FileWriter;
|
|
import java.io.Writer;
|
|
import java.text.DateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* It creates a feed and writes it to a file.
|
|
* <p>
|
|
*
|
|
*/
|
|
public class FeedWriter {
|
|
|
|
private static final DateFormat DATE_PARSER = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
public static void main(String[] args) {
|
|
boolean ok = false;
|
|
if (args.length==2) {
|
|
try {
|
|
String feedType = args[0];
|
|
String fileName = args[1];
|
|
|
|
SyndFeed feed = new SyndFeedImpl();
|
|
feed.setFeedType(feedType);
|
|
|
|
feed.setTitle("Sample Feed (created with ROME)");
|
|
feed.setLink("http://rome.dev.java.net");
|
|
feed.setDescription("This feed has been created using ROME (Java syndication utilities");
|
|
|
|
List entries = new ArrayList();
|
|
SyndEntry entry;
|
|
SyndContent description;
|
|
|
|
entry = new SyndEntryImpl();
|
|
entry.setTitle("ROME v1.0");
|
|
entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome01");
|
|
entry.setPublishedDate(DATE_PARSER.parse("2004-06-08"));
|
|
description = new SyndContentImpl();
|
|
description.setType("text/plain");
|
|
description.setValue("Initial release of ROME");
|
|
entry.setDescription(description);
|
|
entries.add(entry);
|
|
|
|
entry = new SyndEntryImpl();
|
|
entry.setTitle("ROME v2.0");
|
|
entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome02");
|
|
entry.setPublishedDate(DATE_PARSER.parse("2004-06-16"));
|
|
description = new SyndContentImpl();
|
|
description.setType("text/plain");
|
|
description.setValue("Bug fixes, minor API changes and some new features");
|
|
entry.setDescription(description);
|
|
entries.add(entry);
|
|
|
|
entry = new SyndEntryImpl();
|
|
entry.setTitle("ROME v3.0");
|
|
entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome03");
|
|
entry.setPublishedDate(DATE_PARSER.parse("2004-07-27"));
|
|
description = new SyndContentImpl();
|
|
description.setType("text/html");
|
|
description.setValue("<p>More Bug fixes, mor API changes, some new features and some Unit testing</p>"+
|
|
"<p>For details check the <a href=\"https://rometools.jira.com/wiki/display/ROME/Change+Log#ChangeLog-Changesmadefromv0.3tov0.4\">Changes Log</a></p>");
|
|
entry.setDescription(description);
|
|
entries.add(entry);
|
|
|
|
feed.setEntries(entries);
|
|
|
|
Writer writer = new FileWriter(fileName);
|
|
SyndFeedOutput output = new SyndFeedOutput();
|
|
output.output(feed,writer);
|
|
writer.close();
|
|
|
|
System.out.println("The feed has been written to the file ["+fileName+"]");
|
|
|
|
ok = true;
|
|
}
|
|
catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
System.out.println("ERROR: "+ex.getMessage());
|
|
}
|
|
}
|
|
|
|
if (!ok) {
|
|
System.out.println();
|
|
System.out.println("FeedWriter creates a RSS/Atom feed and writes it to a file.");
|
|
System.out.println("The first parameter must be the syndication format for the feed");
|
|
System.out.println(" (rss_0.90, rss_0.91, rss_0.92, rss_0.93, rss_0.94, rss_1.0 rss_2.0 or atom_0.3)");
|
|
System.out.println("The second parameter must be the file name for the feed");
|
|
System.out.println();
|
|
}
|
|
}
|
|
|
|
}
|
|
</pre></div></div>
|
|
</div>
|
|
</div>
|
|
<div class="clear">
|
|
<hr/>
|
|
</div>
|
|
<div id="footer">
|
|
<div class="xright">
|
|
Copyright © 2004-2013
|
|
<a href="http://www.rometools.org">ROME Project</a>.
|
|
All Rights Reserved.
|
|
|
|
</div>
|
|
<div class="clear">
|
|
<hr/>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |