您的位置:首页 > 其它

rome实现rss订阅与发布

2013-07-11 01:28 302 查看
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

<!-- XML版本和字符集 -->

  <?xml version="1.0"?>

  <!-- RSS版本 -->

  <rss version="2.0">

  <!-- 以下为频道信息及新闻列表 -->

  <channel>

  <!-- 频道总体信息:开始 -->

  <!-- 频道标题 -->

  <title>Lift Off News</title>

  <!-- 频道链接的总地址 -->

  <link>http://liftoff.msfc.nasa.gov/</link>

  <!-- 频道描述文字 -->

  <description>Liftoff to Space Exploration.</description>

  <!-- 频道使用的语言(zh-cn表示简体中文) -->

  <language>en-us</language>

  <!-- 频道发布的时间 -->

  <pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>

  <!-- 频道最后更新的时间-->

  <lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate>

  <docs>http://blogs.law.harvard.edu/tech/rss</docs>

  <!-- 频道生成器 -->

  <generator>Weblog Editor 2.0</generator>

  <managingEditor>editor@example.com</managingEditor>

  <webMaster>webmaster@example.com</webMaster>

  <ttl>5</ttl>

  <!-- 频道总体信息:结束 -->

  <!-- 每条RSS新闻信息都包含在item节点中, -->

  <item>

  <!-- 新闻标题 -->

  <title>Star City</title>

  <!-- 新闻链接地址 -->

  <link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>

  <!-- 新闻内容简要描述 -->

  <description>How do Americans get ready to work with Russians aboard the

  International Space Station? They take a crash course in culture, language

  and protocol at Russia's Star City.</description>

  <!-- 新闻发布时间 -->

  <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>

  <!-- 新闻目录 -->

  <category>IT</category>

  <!-- 新闻作者 -->

  <author>bill</author>

  <guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>

  </item>

  <item>

  <title>Space Exploration</title>

  <link>http://liftoff.msfc.nasa.gov/</link>

  <description>Sky watchers in Europe, Asia, and parts of Alaska and Canada

  will experience a partial eclipse of the Sun on Saturday, May 31st.</description>

  <pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate>

  <guid>http://liftoff.msfc.nasa.gov/2003/05/30.html#item572</guid>

  </item>

  <item>

  <title>The Engine That Does More</title>

  <link>http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp</link>

  <description>Before man travels to Mars, NASA hopes to design new engines

  that will let us fly through the Solar System more quickly. The proposed

  VASIMR engine would do that.</description>

  <pubDate>Tue, 27 May 2003 08:37:32 GMT</pubDate>

  <guid>http://www.zhanghangfeng.cn/rss.xml</guid>

  </item>

  <item>

  <title>Astronauts' Dirty Laundry</title>

  <link>http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp</link>

  <description>Compared to earlier spacecraft, the International Space

  Station has many luxuries, but laundry facilities are not one of them.

  Instead, astronauts have other options.</description>

  <pubDate>Tue, 20 May 2003 08:56:02 GMT</pubDate>

  <guid>http://liftoff.msfc.nasa.gov/2003/05/20.html#item570</guid>

  </item>

  </channel>

  </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

public class RssPublish {

/**

* 使用类Channel & Item

*/

public void ChannleItemXml() {

DateFormat dateParse = new SimpleDateFormat("yyyy-MM-dd");

Date date = new Date();

Channel channel = new Channel("rss_2.0"); // 该type参数为固定格式

channel.setTitle("test rome channel title");

channel.setDescription("channel的描述");

channel.setLink("http://hi.baidu.com/openj/rss");

channel.setPubDate(date);

channel.setEncoding("GBK");

List items = new ArrayList();

Item item = new Item();

item.setAuthor("zhangwei");

item.setTitle("item title");

Description desc = new Description();

desc.setType("item desc type");

desc.setValue("item desc value");

item.setDescription(desc);

items.add(item);// 添加一个item

Item item2 = new Item();

item2.setAuthor("zhangwei");

item2.setTitle("use rome to read rss");

Description desc2 = new Description();

desc2.setValue("you must import rome_1.0.jar & jdom.jar");

item2.setDescription(desc2);

Content content = new Content();

content.setValue("rome是用来发布读取rss的工具,遵循rss标准的XML");

item2.setContent(content);

items.add(item2);// 添加一个item

channel.setItems(items);

WireFeedOutput out = new WireFeedOutput();

try {

// Channel继承与WireFeed

System.out.println(out.outputString(channel));

} catch (FeedException ex) {

ex.printStackTrace();

}

}

/**

* 使用接口SyndFeed & SyndEntry

*/

public void SyndFeedXml() {

DateFormat dateParse = new SimpleDateFormat("yyyy-MM-dd");

Date date = new Date();

SyndFeed feed = new SyndFeedImpl();

feed.setFeedType("rss_2.0"); // 该type参数为固定格式

feed.setTitle("test rome channel title");

feed.setDescription("channel的描述");

feed.setLink("http://hi.baidu.com/openj/rss");

feed.setPublishedDate(date);

List items = new ArrayList();

SyndEntry entry = new SyndEntryImpl();

entry.setAuthor("zhangwei");

entry.setTitle("item title");

SyndContent desc = new SyndContentImpl();

desc.setType("item desc type");

desc.setValue("item desc value");

entry.setDescription(desc);

items.add(entry);// 添加一个entry

entry = new SyndEntryImpl();

entry.setAuthor("zhangwei");

entry.setTitle("use rome to read rss");

desc = new SyndContentImpl();

desc.setType("plain/text");

desc.setValue("you must import rome_1.0.jar & jdom.jar");

entry.setDescription(desc);

items.add(entry);// 添加一个entry

feed.setEntries(items);

SyndFeedOutput out = new SyndFeedOutput();

try {

System.out.println(out.outputString(feed));

} catch (FeedException ex) {

ex.printStackTrace();

}

}

}

6.4)Rome实现代理订阅RSS

[java] view plaincopy

public class RssReader

{

public void ReadFeedXml()

{

try

{

System.setProperty("http.proxyHost", "10.191.131.13");

System.setProperty("http.proxyPort", "3128");

String authStr = "account:password";

String auth = "Basic " + new BASE64Encoder().encode(authStr.getBytes());

URL feedurl = new URL("http://rss.sina.com.cn/news/marquee/ddt.xml"); //指定rss位置

URLConnection uc = feedurl.openConnection();

//设定代理

uc.setRequestProperty("Proxy-Authorization", auth);

uc.addRequestProperty("Referer", "localhost");

SyndFeedInput input = new SyndFeedInput();

SyndFeed feed = input.build(new XmlReader(uc));

List entries = feed.getEntries();

for (int i = 0; i < entries.size(); i++)

{

SyndEntry entry = (SyndEntry) entries.get(i);

System.out.print(entry.getTitle());

System.out.print(entry.getLink());

}

}

catch (Exception ex)

{

ex.printStackTrace();

}

}

}

参考:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: