rome/RssAndAtOMUtilitiEsROMEV0.5AndAboveTutorialsAndArticles/RssAndAtOMUtilitiEsROMEV0.5TutorialUsingROMEToReadASyndicationFeed.html

161 lines
No EOL
7.7 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 read 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>
&nbsp;| <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 read a syndication feed<a name="Rss_and_atOM_utilitiEs_ROME_v0.5_Tutorial_Using_ROME_to_read_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>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 char based input stream 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>The default SyndFeed implementation has a detailed and clear toString() implementation. The following line just prints it to the application's output.</p>
<div class="source">
<pre>
System.out.println(feed);
</pre></div>
<p>Following is the full code for a Java application that reads a syndication feed and prints the SyndFeed bean to the application's output.</p>
<div class="source">
<pre>
package com.sun.syndication.samples;
import java.net.URL;
import java.io.InputStreamReader;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
/**
* It Reads and prints any RSS/Atom feed type.
* &lt;p&gt;
* @author Alejandro Abdelnur
*
*/
public class FeedReader {
public static void main(String[] args) {
boolean ok = false;
if (args.length==1) {
try {
URL feedUrl = new URL(args[0]);
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedUrl));
System.out.println(feed);
ok = true;
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println(&quot;ERROR: &quot;+ex.getMessage());
}
}
if (!ok) {
System.out.println();
System.out.println(&quot;FeedReader reads and prints any RSS/Atom feed type.&quot;);
System.out.println(&quot;The first parameter must be the URL of the feed to read.&quot;);
System.out.println();
}
}
}
</pre></div></div>
</div>
</div>
<div class="clear">
<hr/>
</div>
<div id="footer">
<div class="xright">
Copyright &#169; 2004-2013
<a href="http://www.rometools.org">ROME Project</a>.
All Rights Reserved.
</div>
<div class="clear">
<hr/>
</div>
</div>
</body>
</html>