Rome的两种使用方法
所需jar包的下载 here,在这里可以找到与rome相关的所有文件
1、只使用rome
package com.ivo.rss;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
public class Feed {
public static void main(String[] args) throws Exception {
SyndFeedInput input = new SyndFeedInput();
//System.out.println(warmedFeed);
SyndFeed feed = input.build(new XmlReader(new URL("
http://rss.news.yahoo.com/rss/topstories
")));
// Iterate through feed items, adding a footer each item
Iterator entryIter = feed.getEntries().iterator();
while (entryIter.hasNext()) {
SyndEntry entry = (SyndEntry) entryIter.next();
System.out.println(entry.getPublishedDate());
System.out.println(entry.getTitle());
//System.out.println(entry.getDescription());
//System.out.println(entry.getAuthor());
System.out.println(entry.getLink());
}
}
}
2、使用rome+rome fetcher
package com.ivo.rss;
import java.net.URL;
import java.util.List;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.fetcher.FeedFetcher;
import com.sun.syndication.fetcher.impl.FeedFetcherCache;
import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache;
import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher;
public class RssReader {
public static void main(String[] args) throws Exception {
FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance();
FeedFetcher feedFetcher = new HttpURLFeedFetcher(feedInfoCache);
SyndFeed feed = feedFetcher.retrieveFeed(new URL(
"
http://feeds.feedburner.com/jscud
"));
List entryList = feed.getEntries();
for (int i = 0; i < entryList.size(); i++) {
SyndEntry entry = (SyndEntry) entryList.get(i);
System.out.println("Published Date: "+entry.getPublishedDate());
System.out.println("Title: "+entry.getTitle());
System.out.println("Link: "+entry.getLink());
//System.out.println(entry.getDescription());
SyndContent sc = entry.getDescription();
System.out.println("Description: "+sc.getValue());
System.out.println("------------------------------");
}
}
}
上面是使用了缓存的,也就是说更新才读取,如果每次都读取,则修改对应行为:
FeedFetcher feedFetcher = new HttpURLFeedFetcher();