rome实现rss订阅与发布


1. 什么是RSS

RSS也叫聚合RSS,是在线共享内容的一种简易方式(也叫聚合内容, 简易供稿,Really Simple Syndication(真正简单的聚合 ))。通常在时效性比较强的内容上使用RSS订阅能更快速获取信息,网站提供RSS输出,有利于让用户获取网站内容的最新更新. RSS是XML的一种。所有的RSS文档都遵循XML 1.0规范,该规范发布在W3C网站上。拿一个新闻网站来说,如果这个网站使用了RSS技术,也即表示这个网站按照RSS标准将其新闻罗列出来,生成一个RSS文件通常称为(RSS Feed,其实也就是一个*.xml文件)。其他网站或个人就订阅该新闻网站的新闻 ,其实也就是读取了新闻网站提供的RSS文件,既然说是”订阅“即表示这个过程是动态的,即主要体现在新闻网站上的内容更新了,浏览者也同时得到了更新后 的内容。当然这要借助工具。

2. RSS用途

1) .订阅BLOG   

可以订阅工作中所需的技术文章,也可以订阅与你有共同爱好的作者的Blog,总之,对什么感兴趣就可以订什么。

2) 订阅新闻  

无论是奇闻怪事、明星消息、体坛风云,只要你想知道的,都可以订阅。 你再也不用一个网站一个网站,一个网页一个网页去逛了。只要这将你需要的内容订阅在一个RSS阅读器中,这些内容就会自动出现你的阅读器里,你也不必为了一个急切想知道的消息而不断的刷新网页,因为一旦有了更新,RSS阅读器就会自己通知你

3. RSS标准

1) 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 2.0代码样例

[xhtml]view plaincopy
  1. <!--XML版本和字符集-->
  2.   <?xmlversion="1.0"?>
  3.   <!--RSS版本-->
  4.   <rssversion="2.0">
  5.   <!--以下为频道信息及新闻列表-->
  6.   <channel>
  7.   <!--频道总体信息:开始-->
  8.   <!--频道标题-->
  9.   <title>LiftOffNews</title>
  10.   <!--频道链接的总地址-->
  11.   <link>http://liftoff.msfc.nasa.gov/</link>
  12.   <!--频道描述文字-->
  13.   <description>LiftofftoSpaceExploration.</description>
  14.   <!--频道使用的语言(zh-cn表示简体中文)-->
  15.   <language>en-us</language>
  16.   <!--频道发布的时间-->
  17.   <pubDate>Tue,10Jun200304:00:00GMT</pubDate>
  18.   <!--频道最后更新的时间-->
  19.   <lastBuildDate>Tue,10Jun200309:41:01GMT</lastBuildDate>
  20.   <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  21.   <!--频道生成器-->
  22.   <generator>WeblogEditor2.0</generator>
  23.   <managingEditor>editor@example.com</managingEditor>
  24.   <webMaster>webmaster@example.com</webMaster>
  25.   <ttl>5</ttl>
  26.   <!--频道总体信息:结束-->
  27.   <!--每条RSS新闻信息都包含在item节点中,-->
  28.   <item>
  29.   <!--新闻标题-->
  30.   <title>StarCity</title>
  31.   <!--新闻链接地址-->
  32.   <link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
  33.   <!--新闻内容简要描述-->
  34.   <description>HowdoAmericansgetreadytoworkwithRussiansaboardthe
  35.   InternationalSpaceStation?Theytakeacrashcourseinculture,language
  36.   andprotocolatRussia'sStarCity.</description>
  37.   <!--新闻发布时间-->
  38.   <pubDate>Tue,03Jun200309:39:21GMT</pubDate>
  39.   <!--新闻目录-->
  40.   <category>IT</category>
  41.   <!--新闻作者-->
  42.   <author>bill</author>
  43.   <guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>
  44.   </item>
  45.   <item>
  46.   <title>SpaceExploration</title>
  47.   <link>http://liftoff.msfc.nasa.gov/</link>
  48.   <description>SkywatchersinEurope,Asia,andpartsofAlaskaandCanada
  49.   willexperienceapartialeclipseoftheSunonSaturday,May31st.</description>
  50.   <pubDate>Fri,30May200311:06:42GMT</pubDate>
  51.   <guid>http://liftoff.msfc.nasa.gov/2003/05/30.html#item572</guid>
  52.   </item>
  53.   <item>
  54.   <title>TheEngineThatDoesMore</title>
  55.   <link>http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp</link>
  56.   <description>BeforemantravelstoMars,NASAhopestodesignnewengines
  57.   thatwillletusflythroughtheSolarSystemmorequickly.Theproposed
  58.   VASIMRenginewoulddothat.</description>
  59.   <pubDate>Tue,27May200308:37:32GMT</pubDate>
  60.   <guid>http://www.zhanghangfeng.cn/rss.xml</guid>
  61.   </item>
  62.   <item>
  63.   <title>Astronauts'DirtyLaundry</title>
  64.   <link>http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp</link>
  65.   <description>Comparedtoearlierspacecraft,theInternationalSpace
  66.   Stationhasmanyluxuries,butlaundryfacilitiesarenotoneofthem.
  67.   Instead,astronautshaveotheroptions.</description>
  68.   <pubDate>Tue,20May200308:56:02GMT</pubDate>
  69.   <guid>http://liftoff.msfc.nasa.gov/2003/05/20.html#item570</guid>
  70.   </item>
  71.   </channel>
  72.   </rss>

4. 如何实现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.

5. Rome简介

rome是dev.java.net下的一个开源的项目,是一个“解析、创建、发布RSS和ATOM格式”的工具集,支持RSS 0.90, RSS 0.91 Netscape, RSS 0.91 Userland, RSS 0.92, RSS 0.93, RSS 0.94, RSS 1.0, RSS 2.0, Atom 0.3, and Atom 1.0 等众多版本,对rss和atom中的各个模块都进行了很好的封装,功能很强大。

6. 用 Rome 实现 RSS 服务

6.1). 环境准备要使用Rome技术必须在工程中包含以下两个jar包: jdom-1.0.jar 和rome-1.0.jar,且jdom的版本也必须相对应.

6.2). Rome类关系模型

6.3)发布Rss,格式符合RSS标准的要求

[java]view plaincopy
  1. publicclassRssPublish{
  2. /**
  3. *使用类Channel&Item
  4. */
  5. publicvoidChannleItemXml(){
  6. DateFormatdateParse=newSimpleDateFormat("yyyy-MM-dd");
  7. Datedate=newDate();
  8. Channelchannel=newChannel("rss_2.0");//该type参数为固定格式
  9. channel.setTitle("testromechanneltitle");
  10. channel.setDescription("channel的描述");
  11. channel.setLink("http://hi.baidu.com/openj/rss");
  12. channel.setPubDate(date);
  13. channel.setEncoding("GBK");
  14. Listitems=newArrayList();
  15. Itemitem=newItem();
  16. item.setAuthor("zhangwei");
  17. item.setTitle("itemtitle");
  18. Descriptiondesc=newDescription();
  19. desc.setType("itemdesctype");
  20. desc.setValue("itemdescvalue");
  21. item.setDescription(desc);
  22. items.add(item);//添加一个item
  23. Itemitem2=newItem();
  24. item2.setAuthor("zhangwei");
  25. item2.setTitle("userometoreadrss");
  26. Descriptiondesc2=newDescription();
  27. desc2.setValue("youmustimportrome_1.0.jar&jdom.jar");
  28. item2.setDescription(desc2);
  29. Contentcontent=newContent();
  30. content.setValue("rome是用来发布读取rss的工具,遵循rss标准的XML");
  31. item2.setContent(content);
  32. items.add(item2);//添加一个item
  33. channel.setItems(items);
  34. WireFeedOutputout=newWireFeedOutput();
  35. try{
  36. //Channel继承与WireFeed
  37. System.out.println(out.outputString(channel));
  38. }catch(FeedExceptionex){
  39. ex.printStackTrace();
  40. }
  41. }
  42. /**
  43. *使用接口SyndFeed&SyndEntry
  44. */
  45. publicvoidSyndFeedXml(){
  46. DateFormatdateParse=newSimpleDateFormat("yyyy-MM-dd");
  47. Datedate=newDate();
  48. SyndFeedfeed=newSyndFeedImpl();
  49. feed.setFeedType("rss_2.0");//该type参数为固定格式
  50. feed.setTitle("testromechanneltitle");
  51. feed.setDescription("channel的描述");
  52. feed.setLink("http://hi.baidu.com/openj/rss");
  53. feed.setPublishedDate(date);
  54. Listitems=newArrayList();
  55. SyndEntryentry=newSyndEntryImpl();
  56. entry.setAuthor("zhangwei");
  57. entry.setTitle("itemtitle");
  58. SyndContentdesc=newSyndContentImpl();
  59. desc.setType("itemdesctype");
  60. desc.setValue("itemdescvalue");
  61. entry.setDescription(desc);
  62. items.add(entry);//添加一个entry
  63. entry=newSyndEntryImpl();
  64. entry.setAuthor("zhangwei");
  65. entry.setTitle("userometoreadrss");
  66. desc=newSyndContentImpl();
  67. desc.setType("plain/text");
  68. desc.setValue("youmustimportrome_1.0.jar&jdom.jar");
  69. entry.setDescription(desc);
  70. items.add(entry);//添加一个entry
  71. feed.setEntries(items);
  72. SyndFeedOutputout=newSyndFeedOutput();
  73. try{
  74. System.out.println(out.outputString(feed));
  75. }catch(FeedExceptionex){
  76. ex.printStackTrace();
  77. }
  78. }
  79. }

6.4)Rome实现代理订阅RSS

[java]view plaincopy
  1. publicclassRssReader
  2. {
  3. publicvoidReadFeedXml()
  4. {
  5. try
  6. {
  7. System.setProperty("http.proxyHost","10.191.131.13");
  8. System.setProperty("http.proxyPort","3128");
  9. StringauthStr="account:password";
  10. Stringauth="Basic"+newBASE64Encoder().encode(authStr.getBytes());
  11. URLfeedurl=newURL("http://rss.sina.com.cn/news/marquee/ddt.xml");//指定rss位置
  12. URLConnectionuc=feedurl.openConnection();
  13. //设定代理
  14. uc.setRequestProperty("Proxy-Authorization",auth);
  15. uc.addRequestProperty("Referer","localhost");
  16. SyndFeedInputinput=newSyndFeedInput();
  17. SyndFeedfeed=input.build(newXmlReader(uc));
  18. Listentries=feed.getEntries();
  19. for(inti=0;i<entries.size();i++)
  20. {
  21. SyndEntryentry=(SyndEntry)entries.get(i);
  22. System.out.print(entry.getTitle());
  23. System.out.print(entry.getLink());
  24. }
  25. }
  26. catch(Exceptionex)
  27. {
  28. ex.printStackTrace();
  29. }
  30. }
  31. }

参考:

http://blog.csdn.net/wengyupeng/archive/2008/07/28/2723958.aspx
http://blog.csdn.net/yefei679/archive/2009/03/15/3992851.aspx
http://www.fengfly.com/plus/view-168104-1.html
http://wangcheng.javaeye.com/blog/53778

优质内容筛选与推荐>>
1、在linux CentOS6上安装web环境
2、3.1.3 SQL Server Management Studio 配置
3、递归
4、【C编程基础】多线程编程
5、算法不会,尚能饭否之栈


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn