rome/HowRomeWorks/RomeV0.4TutorialUsingRomeToCreateAndWriteASyndicationFeed.html

391 lines
17 KiB
HTML
Raw Permalink Normal View History

2014-04-18 19:37:21 +02:00
<!DOCTYPE html>
<!--
2016-04-24 20:54:49 +02:00
| Generated by Apache Maven Doxia at 2016-04-24
2014-04-18 19:37:21 +02:00
| Rendered using Apache Maven Fluido Skin 1.3.0
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
2014-04-18 19:37:21 +02:00
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="author" content="mkurz" />
<meta name="Date-Creation-yyyymmdd" content="20110814" />
2016-04-24 20:54:49 +02:00
<meta name="Date-Revision-yyyymmdd" content="20160424" />
<meta http-equiv="Content-Language" content="en" />
2014-04-18 19:37:21 +02:00
<title>ROME - Rome v0.4 Tutorial, Using Rome to create and write a syndication feed</title>
<link rel="stylesheet" href="../css/apache-maven-fluido-1.3.0.min.css" />
<link rel="stylesheet" href="../css/site.css" />
<link rel="stylesheet" href="../css/print.css" media="print" />
<script type="text/javascript" src="../js/apache-maven-fluido-1.3.0.min.js"></script>
</head>
<body class="topBarDisabled">
2016-04-24 20:54:49 +02:00
2016-04-24 20:54:49 +02:00
<a href="http://github.com/rometools/rome">
<img style="position: absolute; top: 0; right: 0; border: 0; z-index: 10000;"
src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"
alt="Fork me on GitHub">
</a>
2014-04-18 19:37:21 +02:00
<div class="container-fluid">
<div id="banner">
<div class="pull-left">
2016-04-24 20:54:49 +02:00
<a href="../index.html" id="bannerLeft">
2014-04-18 19:37:21 +02:00
<img src="../images/romelogo.png" alt="ROME"/>
</a>
2014-04-18 19:37:21 +02:00
</div>
<div class="pull-right"> </div>
<div class="clear"><hr/></div>
</div>
2014-04-18 19:37:21 +02:00
<div id="breadcrumbs">
<ul class="breadcrumb">
2014-04-18 19:37:21 +02:00
2016-04-24 20:54:49 +02:00
<li id="publishDate" class="pull-right">Last Published: 2016-04-24</li> <li class="divider pull-right">|</li>
<li id="projectVersion" class="pull-right">Version: 1.7.0-SNAPSHOT</li>
2014-04-18 19:37:21 +02:00
</ul>
</div>
2014-04-18 19:37:21 +02:00
<div class="row-fluid">
<div id="leftColumn" class="span3">
<div class="well sidebar-nav">
2014-04-18 19:37:21 +02:00
<ul class="nav nav-list">
<li class="nav-header">Rome</li>
<li>
<a href="../index.html" title="Overview">
<i class="none"></i>
Overview</a>
</li>
2014-04-18 19:37:21 +02:00
<li>
<a href="../HowRomeWorks/index.html" title="How Rome Works">
<i class="icon-chevron-down"></i>
How Rome Works</a>
<ul class="nav nav-list">
<li>
<a href="../HowRomeWorks/RomeV0.4TutorialUsingRomeToReadASyndicationFeed.html" title="Read a syndication feed">
<i class="none"></i>
Read a syndication feed</a>
</li>
2014-04-18 19:37:21 +02:00
<li class="active">
<a href="#"><i class="none"></i>Create and write a syndication feed</a>
</li>
2014-04-18 19:37:21 +02:00
<li>
<a href="../HowRomeWorks/RomeV0.4TutorialUsingRomeToConvertASyndicationFeedFromOneTypeToAnother.html" title="Convert a syndication feed">
<i class="none"></i>
Convert a syndication feed</a>
</li>
2014-04-18 19:37:21 +02:00
<li>
<a href="../HowRomeWorks/RomeV0.4TutorialUsingRomeToAggregateManySyndicationFeedsIntoASingleOne.html" title="Aggregate many syndication feeds">
<i class="none"></i>
Aggregate many syndication feeds</a>
</li>
2014-04-18 19:37:21 +02:00
<li>
<a href="../HowRomeWorks/UnderstandingTheRomeCommonClassesAndInterfaces.html" title="Common classes and interfaces">
<i class="none"></i>
Common classes and interfaces</a>
</li>
2014-04-18 19:37:21 +02:00
<li>
<a href="../HowRomeWorks/RomeV0.4TutorialDefiningACustomModuleBeanParserAndGenerator.html" title="Defining a Custom Module">
<i class="none"></i>
Defining a Custom Module</a>
</li>
</ul>
</li>
2014-04-18 19:37:21 +02:00
<li>
<a href="../RssAndAtOMUtilitiEsROMEV0.5AndAboveTutorialsAndArticles/index.html" title="Tutorials And Articles">
<i class="none"></i>
Tutorials And Articles</a>
</li>
2014-04-18 19:37:21 +02:00
<li>
<a href="../ROMEReleases/index.html" title="Releases">
<i class="icon-chevron-right"></i>
Releases</a>
</li>
2014-04-18 19:37:21 +02:00
<li>
<a href="../ROMEDevelopmentProposals/index.html" title="ROME Development Proposals">
<i class="none"></i>
ROME Development Proposals</a>
</li>
2016-04-24 20:54:49 +02:00
<li>
<a href="../Modules/index.html" title="Modules">
<i class="icon-chevron-right"></i>
Modules</a>
</li>
<li>
<a href="../Fetcher/index.html" title="Fetcher">
<i class="icon-chevron-right"></i>
Fetcher</a>
</li>
<li>
<a href="../Opml/index.html" title="OPML">
<i class="none"></i>
OPML</a>
</li>
<li>
<a href="../Propono/index.html" title="Propono">
<i class="none"></i>
Propono</a>
</li>
<li>
<a href="../Certiorem/index.html" title="Certiorem">
<i class="icon-chevron-right"></i>
Certiorem</a>
</li>
2014-04-18 19:37:21 +02:00
<li class="nav-header">Project Documentation</li>
2016-04-24 20:54:49 +02:00
2014-04-18 19:37:21 +02:00
<li>
<a href="../project-info.html" title="Project Information">
<i class="icon-chevron-right"></i>
Project Information</a>
</li>
2014-04-18 19:37:21 +02:00
</ul>
<hr class="divider" />
<div id="poweredBy">
<div class="clear"></div>
<div class="clear"></div>
<div class="clear"></div>
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
2014-04-18 19:37:21 +02:00
<img class="builtBy" alt="Built by Maven" src="../images/logos/maven-feather.png" />
</a>
2014-04-18 19:37:21 +02:00
</div>
</div>
</div>
<div id="bodyColumn" class="span9" >
<div class="section">
2016-04-24 20:54:49 +02:00
<h2>Using Rome to create and write a syndication feed<a name="Using_Rome_to_create_and_write_a_syndication_feed"></a></h2>
<p>Rome represents syndication feeds (RSS and Atom) as instances of the com.rometools.rome.feed.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(&quot;Sample Feed (created with Rome)&quot;);
feed.setLink(&quot;http://rome.dev.java.net&quot;);
feed.setDescription(&quot;This feed has been created using Rome (Java syndication utilities&quot;);
</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(&quot;Rome v1.0&quot;);
entry.setLink(&quot;http://wiki.java.net/bin/view/Javawsxml/Rome01&quot;);
entry.setPublishedDate(DATE_PARSER.parse(&quot;2004-06-08&quot;));
description = new SyndContentImpl();
description.setType(&quot;text/plain&quot;);
description.setValue(&quot;Initial release of Rome&quot;);
entry.setDescription(description);
entries.add(entry);
...
entry = new SyndEntryImpl();
entry.setTitle(&quot;Rome v3.0&quot;);
entry.setLink(&quot;http://wiki.java.net/bin/view/Javawsxml/Rome03&quot;);
entry.setPublishedDate(DATE_PARSER.parse(&quot;2004-07-27&quot;));
description = new SyndContentImpl();
description.setType(&quot;text/html&quot;);
description.setValue(&quot;&lt;p&gt;More Bug fixes, mor API changes, some new features and some Unit testing&lt;/p&gt;&quot;+
&quot;&lt;p&gt;For details check the &lt;a href=\&quot;http://wiki.java.net/bin/view/Javawsxml/RomeChangesLog#RomeV03\&quot;&gt;Changes Log&lt;/a&gt;&lt;/p&gt;&quot;);
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>
2016-04-24 20:54:49 +02:00
package com.rometools.rome.samples;
2016-04-24 20:54:49 +02:00
import com.rometools.rome.feed.synd.*;
import com.rometools.rome.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.
* &lt;p&gt;
*
*/
public class FeedWriter {
private static final DateFormat DATE_PARSER = new SimpleDateFormat(&quot;yyyy-MM-dd&quot;);
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(&quot;Sample Feed (created with Rome)&quot;);
feed.setLink(&quot;http://rome.dev.java.net&quot;);
feed.setDescription(&quot;This feed has been created using Rome (Java syndication utilities&quot;);
List entries = new ArrayList();
SyndEntry entry;
SyndContent description;
entry = new SyndEntryImpl();
entry.setTitle(&quot;Rome v1.0&quot;);
entry.setLink(&quot;http://wiki.java.net/bin/view/Javawsxml/Rome01&quot;);
entry.setPublishedDate(DATE_PARSER.parse(&quot;2004-06-08&quot;));
description = new SyndContentImpl();
description.setType(&quot;text/plain&quot;);
description.setValue(&quot;Initial release of Rome&quot;);
entry.setDescription(description);
entries.add(entry);
entry = new SyndEntryImpl();
entry.setTitle(&quot;Rome v2.0&quot;);
entry.setLink(&quot;http://wiki.java.net/bin/view/Javawsxml/Rome02&quot;);
entry.setPublishedDate(DATE_PARSER.parse(&quot;2004-06-16&quot;));
description = new SyndContentImpl();
description.setType(&quot;text/plain&quot;);
description.setValue(&quot;Bug fixes, minor API changes and some new features&quot;);
entry.setDescription(description);
entries.add(entry);
entry = new SyndEntryImpl();
entry.setTitle(&quot;Rome v3.0&quot;);
entry.setLink(&quot;http://wiki.java.net/bin/view/Javawsxml/Rome03&quot;);
entry.setPublishedDate(DATE_PARSER.parse(&quot;2004-07-27&quot;));
description = new SyndContentImpl();
description.setType(&quot;text/html&quot;);
description.setValue(&quot;&lt;p&gt;More Bug fixes, mor API changes, some new features and some Unit testing&lt;/p&gt;&quot;+
&quot;&lt;p&gt;For details check the &lt;a href=\&quot;http://wiki.java.net/bin/view/Javawsxml/RomeChangesLog#RomeV03\&quot;&gt;Changes Log&lt;/a&gt;&lt;/p&gt;&quot;);
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(&quot;The feed has been written to the file [&quot;+fileName+&quot;]&quot;);
ok = true;
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println(&quot;ERROR: &quot;+ex.getMessage());
}
}
if (!ok) {
System.out.println();
System.out.println(&quot;FeedWriter creates a RSS/Atom feed and writes it to a file.&quot;);
System.out.println(&quot;The first parameter must be the syndication format for the feed&quot;);
System.out.println(&quot; (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)&quot;);
System.out.println(&quot;The second parameter must be the file name for the feed&quot;);
System.out.println();
}
}
}
</pre></div></div>
</div>
2014-04-18 19:37:21 +02:00
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
2016-04-24 20:54:49 +02:00
<div class="row span12">Copyright &copy; 2016.
2014-04-18 19:37:21 +02:00
All Rights Reserved.
</div>
2014-04-18 19:37:21 +02:00
</div>
</footer>
</body>
2016-04-24 20:54:49 +02:00
</html>