zoukankan      html  css  js  c++  java
  • 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.   <?xml version="1.0"?>   
    3.   <!-- RSS版本 -->   
    4.   <rss version="2.0">   
    5.   <!-- 以下为频道信息及新闻列表 -->   
    6.   <channel>   
    7.   <!-- 频道总体信息:开始 -->   
    8.   <!-- 频道标题 -->   
    9.   <title>Lift Off News</title>   
    10.   <!-- 频道链接的总地址 -->   
    11.   <link>http://liftoff.msfc.nasa.gov/</link>   
    12.   <!-- 频道描述文字 -->   
    13.   <description>Liftoff to Space Exploration.</description>   
    14.   <!-- 频道使用的语言(zh-cn表示简体中文) -->   
    15.   <language>en-us</language>   
    16.   <!-- 频道发布的时间 -->   
    17.   <pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>   
    18.   <!-- 频道最后更新的时间-->   
    19.   <lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate>   
    20.   <docs>http://blogs.law.harvard.edu/tech/rss</docs>   
    21.   <!-- 频道生成器 -->   
    22.   <generator>Weblog Editor 2.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>Star City</title>   
    31.   <!-- 新闻链接地址 -->   
    32.   <link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>   
    33.   <!-- 新闻内容简要描述 -->   
    34.   <description>How do Americans get ready to work with Russians aboard the   
    35.   International Space Station? They take a crash course in culture, language   
    36.   and protocol at Russia's Star City.</description>   
    37.   <!-- 新闻发布时间 -->   
    38.   <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</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>Space Exploration</title>   
    47.   <link>http://liftoff.msfc.nasa.gov/</link>   
    48.   <description>Sky watchers in Europe, Asia, and parts of Alaska and Canada   
    49.   will experience a partial eclipse of the Sun on Saturday, May 31st.</description>   
    50.   <pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate>   
    51.   <guid>http://liftoff.msfc.nasa.gov/2003/05/30.html#item572</guid>   
    52.   </item>   
    53.   <item>   
    54.   <title>The Engine That Does More</title>   
    55.   <link>http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp</link>   
    56.   <description>Before man travels to Mars, NASA hopes to design new engines   
    57.   that will let us fly through the Solar System more quickly. The proposed   
    58.   VASIMR engine would do that.</description>   
    59.   <pubDate>Tue, 27 May 2003 08:37:32 GMT</pubDate>   
    60.   <guid>http://www.zhanghangfeng.cn/rss.xml</guid>   
    61.   </item>   
    62.   <item>   
    63.   <title>Astronauts' Dirty Laundry</title>   
    64.   <link>http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp</link>   
    65.   <description>Compared to earlier spacecraft, the International Space   
    66.   Station has many luxuries, but laundry facilities are not one of them.   
    67.   Instead, astronauts have other options.</description>   
    68.   <pubDate>Tue, 20 May 2003 08:56:02 GMT</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. public class RssPublish {  
    2.     /** 
    3.      * 使用类Channel & Item 
    4.      */  
    5.     public void ChannleItemXml() {  
    6.         DateFormat dateParse = new SimpleDateFormat("yyyy-MM-dd");  
    7.         Date date = new Date();  
    8.   
    9.         Channel channel = new Channel("rss_2.0"); // 该type参数为固定格式  
    10.         channel.setTitle("test rome channel title");  
    11.         channel.setDescription("channel的描述");  
    12.         channel.setLink("http://hi.baidu.com/openj/rss");  
    13.         channel.setPubDate(date);  
    14.         channel.setEncoding("GBK");  
    15.   
    16.         List items = new ArrayList();  
    17.         Item item = new Item();  
    18.         item.setAuthor("zhangwei");  
    19.         item.setTitle("item title");  
    20.   
    21.         Description desc = new Description();  
    22.         desc.setType("item desc type");  
    23.         desc.setValue("item desc value");  
    24.         item.setDescription(desc);  
    25.   
    26.         items.add(item);// 添加一个item  
    27.   
    28.         Item item2 = new Item();  
    29.         item2.setAuthor("zhangwei");  
    30.         item2.setTitle("use rome to read rss");  
    31.   
    32.         Description desc2 = new Description();  
    33.         desc2.setValue("you must import rome_1.0.jar & jdom.jar");  
    34.         item2.setDescription(desc2);  
    35.         Content content = new Content();  
    36.         content.setValue("rome是用来发布读取rss的工具,遵循rss标准的XML");  
    37.         item2.setContent(content);  
    38.   
    39.         items.add(item2);// 添加一个item  
    40.   
    41.         channel.setItems(items);  
    42.   
    43.         WireFeedOutput out = new WireFeedOutput();  
    44.         try {  
    45.             // Channel继承与WireFeed  
    46.             System.out.println(out.outputString(channel));  
    47.         } catch (FeedException ex) {  
    48.             ex.printStackTrace();  
    49.         }  
    50.     }  
    51.   
    52.     /** 
    53.      * 使用接口SyndFeed & SyndEntry 
    54.      */  
    55.     public void SyndFeedXml() {  
    56.         DateFormat dateParse = new SimpleDateFormat("yyyy-MM-dd");  
    57.         Date date = new Date();  
    58.   
    59.         SyndFeed feed = new SyndFeedImpl();  
    60.         feed.setFeedType("rss_2.0"); // 该type参数为固定格式  
    61.         feed.setTitle("test rome channel title");  
    62.         feed.setDescription("channel的描述");  
    63.         feed.setLink("http://hi.baidu.com/openj/rss");  
    64.         feed.setPublishedDate(date);  
    65.   
    66.         List items = new ArrayList();  
    67.         SyndEntry entry = new SyndEntryImpl();  
    68.         entry.setAuthor("zhangwei");  
    69.         entry.setTitle("item title");  
    70.   
    71.         SyndContent desc = new SyndContentImpl();  
    72.         desc.setType("item desc type");  
    73.         desc.setValue("item desc value");  
    74.         entry.setDescription(desc);  
    75.   
    76.         items.add(entry);// 添加一个entry  
    77.   
    78.         entry = new SyndEntryImpl();  
    79.         entry.setAuthor("zhangwei");  
    80.         entry.setTitle("use rome to read rss");  
    81.   
    82.         desc = new SyndContentImpl();  
    83.         desc.setType("plain/text");  
    84.         desc.setValue("you must import rome_1.0.jar & jdom.jar");  
    85.         entry.setDescription(desc);  
    86.   
    87.         items.add(entry);// 添加一个entry  
    88.   
    89.         feed.setEntries(items);  
    90.   
    91.         SyndFeedOutput out = new SyndFeedOutput();  
    92.         try {  
    93.             System.out.println(out.outputString(feed));  
    94.         } catch (FeedException ex) {  
    95.             ex.printStackTrace();  
    96.         }  
    97.     }  
    98. }  

    6.4)Rome实现代理订阅RSS

    [java] view plaincopy
     
    1. public class RssReader   
    2.     {   
    3.         public void ReadFeedXml()   
    4.         {   
    5.             try   
    6.             {   
    7.                 System.setProperty("http.proxyHost""10.191.131.13");   
    8.                 System.setProperty("http.proxyPort""3128");   
    9.                 String authStr = "account:password";   
    10.                 String auth = "Basic " + new BASE64Encoder().encode(authStr.getBytes());   
    11.                 URL feedurl = new URL("http://rss.sina.com.cn/news/marquee/ddt.xml"); //指定rss位置   
    12.                 URLConnection uc = feedurl.openConnection();   
    13.                 //设定代理   
    14.                 uc.setRequestProperty("Proxy-Authorization", auth);   
    15.                 uc.addRequestProperty("Referer""localhost");  
    16.                 SyndFeedInput input = new SyndFeedInput();   
    17.                 SyndFeed feed = input.build(new XmlReader(uc));  
    18.                 List entries = feed.getEntries();   
    19.                 for (int i = 0; i < entries.size(); i++)   
    20.                 {   
    21.                     SyndEntry entry = (SyndEntry) entries.get(i);   
    22.                     System.out.print(entry.getTitle());   
    23.                     System.out.print(entry.getLink());   
    24.                 }   
    25.             }   
    26.             catch (Exception ex)   
    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

  • 相关阅读:
    mysql
    Linux下的C语言基础-4
    Linux下的C语言基础-3
    Linux下的C语言基础-2
    Linux下的C语言基础-1
    LeetCode:1375. 灯泡开关 III
    Jenkins+robotframework持续集成环境(三)
    Jenkins+robotframework持续集成环境(二)
    Jenkins+robotframework持续集成环境(一)
    robotframework操作使用
  • 原文地址:https://www.cnblogs.com/chenying99/p/3183157.html
Copyright © 2011-2022 走看看