rome/HowRomeWorks/RomeV0.4TutorialUsingRomeToAggregateManySyndicationFeedsIntoASingleOne.html

336 lines
15 KiB
HTML
Raw 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 aggregate many syndication feeds into a single one</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>
<a href="../HowRomeWorks/RomeV0.4TutorialUsingRomeToCreateAndWriteASyndicationFeed.html" title="Create and write a syndication feed">
<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 class="active">
<a href="#"><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 aggregate many syndication feeds into a single one<a name="Using_Rome_to_aggregate_many_syndication_feeds_into_a_single_one"></a></h2>
<p>Rome represents syndication feeds (RSS and Atom) as instances of the com.rometools.rome.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>Rome includes parsers to process syndication feeds into SyndFeed instances. The SyndFeedInput class handles the parsers using the correct one based on the syndication feed being processed. The developer does not need to worry about selecting the right parser for a syndication feed, the SyndFeedInput will take care of it by peeking at the syndication feed structure. All it takes to read a syndication feed using Rome are the following 2 lines of code:</p>
<div class="source">
<pre>
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedUrl));
</pre></div>
<p>The first line creates a SyndFeedInput instance that will work with any syndication feed type (RSS and Atom versions). The second line instructs the SyndFeedInput to read the syndication feed from the InputStream of a URL pointing to the feed. The XmlReader is a character based Reader that resolves the encoding following the HTTP MIME types and XML rules for it. The SyndFeedInput.build() method returns a SyndFeed instance that can be easily processed.</p>
<p>Rome also includes generators to create syndication feeds out of SyndFeed instances. The SyndFeedOutput class does this generation. The SyndFeedOutput will generate a syndication feed of the feed type indicated by the SyndFeed object being output. The following two lines of code show how to create a syndication feed output from a SyndFeed instance:</p>
<div class="source">
<pre>
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed,new PrintWriter(System.out));
</pre></div>
<p>The first line creates a SyndFeedOutput instance that will produce syndication feeds. It can output feeds of any type (rss_0.9, rss_0.91, rss_0.92, rss_0.93, rss_0.94, rss_1.0, rss_2.0 &amp; atom_0.3), the SyndFeed feedType property indicates the type. The second line writes the SyndFeed as a syndication feed into the application's output.</p>
<p>SyndFeed instances can also be created and populated within the code. For example:</p>
<div class="source">
<pre>
SyndFeed aggrFeed = new SyndFeedImpl();
aggrFeed.setFeedType(&quot;rss_1.0&quot;);
aggrFeed.setTitle(&quot;Aggregated Feed&quot;);
aggrFeed.setDescription(&quot;Anonymous Aggregated Feed&quot;);
aggrFeed.setAuthor(&quot;anonymous&quot;);
aggrFeed.setLink(&quot;http://www.anonymous.com&quot;);
</pre></div>
<p>The snipped of code above creates a SyndFeed instance using the default implementation provided by Rome, sets the feed type to RSS 1.0, sets the title, description, author and link properties of the feed.</p>
<p>SyndFeed properties can be modified, assigned to other SyndFeed instances, removed, etc. It's important to remember that the getters/setters semantics defined for all SyndFeed properties (and properties of its properties) is a copy by reference, not by value. In other words if you alter the property of a SyndFeed property you are altering the SyndFeed. Or, another example, if you assign the list of entries of one SyndFeed instance to another SyndFeed isntance and then you modify the list or any of the entries in the list, you are modifying the entries of both SyndFeed instances.</p>
<p>The following lines of code show how to copy the entries of a SyndFeed instance being read (inFeed) into a SyndFeed instance being created within the application (feed):</p>
<div class="source">
<pre>
2016-04-24 20:54:49 +02:00
package com.rometools.rome.samples;
import java.net.URL;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.List;
import java.util.ArrayList;
2016-04-24 20:54:49 +02:00
import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.feed.synd.SyndFeedImpl;
import com.rometools.rome.io.SyndFeedOutput;
import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.XmlReader;
/**
* It aggregates a list of RSS/Atom feeds (they can be of different types)
* into a single feed of the specified type.
* &lt;p&gt;
* @author Alejandro Abdelnur
*
*/
public class FeedAggregator {
public static void main(String[] args) {
boolean ok = false;
if (args.length&gt;=2) {
try {
String outputType = args[0];
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType(outputType);
feed.setTitle(&quot;Aggregated Feed&quot;);
feed.setDescription(&quot;Anonymous Aggregated Feed&quot;);
feed.setAuthor(&quot;anonymous&quot;);
feed.setLink(&quot;http://www.anonymous.com&quot;);
List entries = new ArrayList();
feed.setEntries(entries);
for (int i=1;i&lt;args.length;i++) {
URL inputUrl = new URL(args[i]);
SyndFeedInput input = new SyndFeedInput();
SyndFeed inFeed = input.build(new XmlReader(inputUrl));
entries.addAll(inFeed.getEntries());
}
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed,new PrintWriter(System.out));
ok = true;
}
catch (Exception ex) {
System.out.println(&quot;ERROR: &quot;+ex.getMessage());
}
}
if (!ok) {
System.out.println();
System.out.println(&quot;FeedAggregator aggregates different feeds into a single one.&quot;);
System.out.println(&quot;The first parameter must be the feed type for the aggregated feed.&quot;);
System.out.println(&quot; [valid values are: rss_0.9, rss_0.91, rss_0.92, rss_0.93, ]&quot;);
System.out.println(&quot; [ rss_0.94, rss_1.0, rss_2.0 &amp; atom_0.3 ]&quot;);
System.out.println(&quot;The second to last parameters are the URLs of feeds to aggregate.&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>