1.RSS标准
RSS标准比较混乱,主要有以下3个系列
- RSS 0.9x / 2.0 : RSS技术诞生于1999年的网景公司(Netscape),其发布了一个0.9版本的规范。2001年,RSS技术标准的发展工作被Userland Software公司的戴夫 温那(Dave Winer)所接手。陆续发布了0.9x的系列版本。当W3C小组发布RSS 1.0后,Dave Winer不承认其有效性。并于2002年9月独自把RSS升级到了2.0版本(Really Simple Syndication),并交由哈佛大学Technology at Harvard Law进行维护。
- RSS 1.0 : 在RSS发展过程中,为使RSS成为一个通用的规范,并进一步标准化。一个联合小组根据W3C新一代的Resource Description Framework (RDF) 对RSS进行了重新定义,发布了RSS 1.0版,并把RSS定义为“RDF Site Summary”。现在RSS 1.0版由W3C联合小组维护。
- Atom : Atom是一个项目的名字,主要是开发一个新的博客摘要格式以解决目前RSS存在的问题(混乱的版本号,不是一个真正的开放标准,表示方法的不一致,定义贫乏等等)。
2.如何实现RSS
RSS标准虽然混乱,但是其本质都是XML文档。你可以只使用notepad, 按照某个RSS标准, 手写一个xml, 并提供给客户端。
现在也有许多开源项目来提供RSS的解决方案。
Rome https://rome.dev.java.net/
RSSLibJ http://enigmastation.com/rsslibj/
RSSLib4J http://devzone.stealthp.org/cms/index.php?page=RSSLib4J
使用这些解决方案可以更方便的处理RSS.
3.用 Rome 实现 RSS 服务
目前Rome最新版本为rome-0.9. 本例是在Struts的Action中实现的RSS服务.
新建一个RssAction
java 代码
- import other classes...
- import com.sun.syndication.feed.synd.SyndFeed;
- import com.sun.syndication.io.SyndFeedOutput;
- public class RssAction extends DispatchAction {
- private static final String MIME_TYPE = "application/xml; charset=UTF-8";
- // Rome中RSS的可选标准
- // rss_0.90, rss_0.91, rss_0.92, rss_0.93, rss_0.94, rss_1.0, rss_2.0, atom_0.3
- private static final String RSS_TYPE = "rss_2.0";
- public ActionForward newsFeed(ActionMapping mapping,
- ActionForm form, HttpServletRequest request,
- HttpServletResponse response) throws Exception {
- NewsFeedBA newsFeedBA = new NewsFeedBA();
- newsFeedBA.doExecute();
- outputRssFeed(response, newsFeedBA.getFeed());
- return null;
- }
- public ActionForward blogFeed(ActionMapping mapping,
- ActionForm form, HttpServletRequest request,
- HttpServletResponse response) throws Exception {
- String uid = request.getParameter("userId");
- BlogFeedBA blogFeedBA = new BlogFeedBA();
- blogFeedBA.setUserId(uid);
- blogFeedBA.doExecute();
- outputRssFeed(response, blogFeedBA.getFeed());
- return null;
- }
- //将SyndFeed写入HttpServletResponse
- private boolean outputRssFeed(HttpServletResponse response, SyndFeed feed) {
- boolean result = false;
- feed.setFeedType(RSS_TYPE);
- response.setContentType(MIME_TYPE);
- SyndFeedOutput output = new SyndFeedOutput();
- try {
- output.output(feed, response.getWriter());
- result = true;
- } catch (IOException e) {
- e.printStackTrace();
- } catch (FeedException e) {
- e.printStackTrace();
- }
- return result;
- }
- }
然后在业务逻辑中,查询数据库,用返回的数据组织相应的Feed.
java 代码
- public SyndFeed createFeed(List news) {
- SyndFeed feed = new SyndFeedImpl();
- feed.setTitle("My RSS Service : news ");
- feed.setLink("http://www.myHomePage.com");
- feed.setDescription("My first RSS service .");
- feed.setEntries(getEntries(news));
- return feed;
- }
- private List getEntries(List news) {
- List entries = new ArrayList();
- SyndEntry entry;
- SyndContent description;
- for (NewsDTO dto : news) {
- entry = new SyndEntryImpl();
- entry.setTitle(dto.getTitle());
- entry.setLink(dto.getLink());
- entry.setPublishedDate(new Date());
- description = new SyndContentImpl();
- description.setType("text/html");
- description.setValue(dto.getContent());
- entry.setDescription(description);
- entries.add(entry);
- }
- return entries;
- }
在struts-config.xml中配置RssAction
xml 代码
- <action path="/rss" type="com.web.action.RssAction"
- parameter="method"
- scope="request">
- action>
启动Tomcat,并访问/rss.do?method=newsFeed 就可以得到新闻的RSS
访问/rss.do?method=blogFeed&userId=123 就可以得到123的blog的RSS了。
现在IE7和Opera都集成了RSS功能。
<script type=text/javascript> addthis_pub= 'wolfit'; addthis_logo= 'http://download.rainbowsoft.org/image/common/zbloglogo.gif'; addthis_brand= 'Z-Blog'; addthis_options= 'favorites, delicious, twitter, digg, myspace, facebook, google, live, email, more'; </script>