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代码样例
<!-- 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>

ROME是一个基于Java的开源程序包。ROME提供了一组Atom/RSS工具集,以使得用Java可以更方便和快捷的来操作大部分 Syndication格式数据。目前最新版本为0.9,已经支持的格式包括: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 和 Atom 1.0。

ROME包含了一组解析器和生成器,以使得在程序中不仅能够方便的解析各种格式的数据,也可以方便的生成出所指定格式的数据。ROME的解析器和生成器是配合使用的,解析器在解析完源数据后,会生成一个ROME提供的中间对象(SyndFeed),这个对象包括了源数据中的所有信息,然后生成器以这个对象(SyndFeed)作为媒介,输出成程序所指定的数据格式。
public class RomeTest {
private static String index = "http://rss.sina.com.cn/roll/sports/hot_roll.xml";
//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 static void main(String[] args){
read();
}

@SuppressWarnings("unchecked")
public static void read(){
try{
URL url = new URL(index);
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(url));
List<SyndEntry> entryties = feed.getEntries();
for(SyndEntry entry:entryties){
System.out.println(feed.getFeedType());//rss类型
System.out.println(entry.getPublishedDate());//发布时间
System.out.println(entry.getAuthor());//作者
System.out.println(entry.getTitle());//标题
System.out.println(entry.getLink());//链接
}
}catch (Exception e) {
e.printStackTrace();
}

}

public static void write(){
// 构造一个SyndFeed新对象
SyndFeed feed = new SyndFeedImpl();
// 设置Feed类型
feed.setFeedType(RSS_TYPE);
// 设置Feed基本信息
feed.setTitle("测试Feed");
feed.setLink("http://www.thedevlog.com");
feed.setDescription("测试使用ROME来构造Feed数据");
// 构造Feed items列表
List<SyndEntry> entries = new ArrayList<SyndEntry>();
SyndEntry entry;
SyndContent description;
// 构造一个新的节点并添加到列表中
entry = new SyndEntryImpl();
entry.setTitle("ROME v1.0");
entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome01");
entry.setPublishedDate(new Date());

description = new SyndContentImpl();
description.setType("text/plain");
description.setValue("Initial release of ROME");

entry.setDescription(description);
entries.add(entry);
// 设置更多的节点...
// 将节点列表赋给feed对象
feed.setEntries(entries);
// 输出feed
Writer writer;
try {
writer = new PrintWriter("./test.xml","UTF-8");
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed, writer);
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值