首先需要准备下环境(需要的jar包):
rome-1.0.jar (下载地址:http://download.csdn.net/detail/y397126710/4323863)
jdom-1.0.jar (下载地址:http://download.csdn.net/detail/y397126710/4323870)
这两个包的版本必须一致,建议全部使用1.0版本。网上虽然有提供了很多下载地址,但是基本很多都是不能下载的,为方便我放自己的空间里方便大家下载。
至于RSS的详细的东西我就不赘述了,网上类似的文章描述的比我好的多了。
大家或许更想知道一个RSS订阅是怎么一回事,首先,RSS订阅需要一个RSS文件,RSS的格式是XML。必须符合XML 1.0规范。
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<title>Sample Feed (created with Rome)</title>
<link>http://rome.dev.java.net</link>
<description>This feed has been created using Rome (Java syndication utilities</description>
<language>en-us</language>
<image>
<title>The image title goes here</title>
<url>http://www.urlofthechannel.com/images/logo.gif</url>
<link>http://www.urlofthechannel.com/</link>
</image>
<item>
<title>Rome v1.0</title>
<link>http://wiki.java.net/bin/view/Javawsxml/Rome01</link>
<description>Initial release of Rome</description>
<pubDate>Mon, 07 Jun 2004 16:00:00 GMT</pubDate>
<guid>http://wiki.java.net/bin/view/Javawsxml/Rome01</guid>
<dc:date>2004-06-07T16:00:00Z</dc:date>
</item>
<item>
<title>Rome v2.0</title>
<link>http://wiki.java.net/bin/view/Javawsxml/Rome02</link>
<description>Bug fixes, <xml>XML</xml> minor API changes and some new features</description>
<pubDate>Tue, 15 Jun 2004 16:00:00 GMT</pubDate>
<guid>http://wiki.java.net/bin/view/Javawsxml/Rome02</guid>
<dc:date>2004-06-15T16:00:00Z</dc:date>
</item>
<item>
<title>Rome v3.0</title>
<link>http://wiki.java.net/bin/view/Javawsxml/Rome03</link>
<description><p>More Bug fixes, mor API changes, some new features and some Unit testing</p><p>For details check the <a href="Changes'>http://wiki.java.net/bin/view/Javawsxml/RomeChangesLog#RomeV03">Changes Log</a></p></description>
<pubDate>Mon, 26 Jul 2004 16:00:00 GMT</pubDate>
<guid>http://wiki.java.net/bin/view/Javawsxml/Rome03</guid>
<dc:date>2004-07-26T16:00:00Z</dc:date>
</item>
</channel>
</rss>
频道(channel)用<channel>表示
频道一般有三个元素,提供关于频道本身的信息:
<title>:频道或提要的名称。
<link>:与该频道关联的 Web 站点或者站点区域的 URL。
<description>:简要介绍该频道是做什么的。
许多频道子元素都是可选的。
此外还可以使用许多其他可选的频道元素。多数都是不言自明的:
<language>:en-us
<copyright>:Copyright 2003, James Lewin
<managingEditor>:dan@spam_me.com (Dan Deletekey)
<webMaster>:dan@spam_me.com (Dan Deletekey)
<pubDate>:Sat, 15 Nov 2003 0:00:01 GMT
<lastBuildDate>:Sat, 15 Nov 2003 0:00:01 GMT
<category>:ebusiness
<generator>:Your CMS 2.0
<docs>:http://blogs.law.harvard.edu/tech/rss
<cloud>:允许进程注册为“cloud”,频道更新时通知它,为 RSS 提要实现了一种轻量级的发布-订阅协议。
<ttl>:存活时间 是一个数字,表示提要在刷新之前缓冲的分钟数。
<rating>:关于该频道的 PICS 评价。
<textInput>:定义可与频道一起显示的输入框。
<skipHours>:告诉聚集器哪些小时的更新可以忽略。
<skipDays>:告诉聚集器那一天的更新可以忽略。
摘要(feed)用<item>表示
每个摘要通常包含三个元素:
<title>:这是项的名称,在标准应用中被转换成 HTML 中的标题。
<link>:这是该项的 URL。title 通常作为一个链接,指向包含在 <link> 元素中的 URL
<description>:通常作为 link 中所指向的 URL 的摘要或者补充。
所有的元素都是可选的,但是一个项至少要么 包含一个 <title>,要么包含一个 <description>。
项还有其他一些可选的元素:
<author>:作者的 e-mail 地址。
<category>:支持有组织的记录。
<comments>:关于项的注释页的 URL。
<enclosure>:支持和该项有关的媒体对象。
<guid>:唯一与该项联系在一起的永久性链接。
<pubDate>:该项是什么时候发布的。
<source>:该项来自哪个 RSS 频道
下面就贴一点我自己写的demo吧。。。
1、发布一个RSS,通过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();
}
}
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);
feed.setEncoding("GBK");
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();
}
}
3、读取外部的RSS
public void ReadFeedXml() {
try {
System.setProperty("http.proxyHost", "10.191.131.13");
System.setProperty("http.proxyPort", "3128");
String authStr = "account:password";
String auth = "Basic " + 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));
feed.setEncoding("GBK");
// 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());
// }
SyndFeedOutput out = new SyndFeedOutput();
try {
System.out.println(out.outputString(feed));
} catch (FeedException ex) {
ex.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
其实我也是花了一天时间研究这一块,也研究的不是很深。因为我实在烦了网上各种没有的关于完成RSS的代码,因此自己才决定写这篇文章的。。。有什么不对的地方,高手看到希望指正。。。
对了,还少了一点,也是我一开始纠结的时间比较长的。就是如何把发布的RSS订阅订阅到像QQ邮箱、google等地的方法,其实上网查了好久,其实也蛮简单的。。
我能找到的就是这几个了,希望有找到的朋友帮忙补充下。。。。
1、添加订阅到Google
打开http://www.google.com/intl/zh-cn/webmasters/add.html,选择按钮样式并输入你的RSS地址,会生成一段代码,把这段代码加入到你的网页即可
2、添加订阅到鲜果
打开http://xianguo.com/tools/sub_button,选择按钮样式并输入你的RSS地址,把生成的代码加入到你的网页之中
3、添加订阅到有道
打开http://reader.youdao.com/tools.html,选择按钮样式并输入你的RSS地址,把生成的代码加入到你的网页之中
4、订阅到QQ邮箱
我没有找到QQ邮箱添加订阅的代码生成页面,不过研究发现,你只要把链接http://mail.qq.com/cgi-bin/feed?u=http://domety.com/feed中的http://domety.com/feed改成你的RSS地址
下面是我做的一个例子,不过RSS源我链接的是新浪
<html>
<head>
<title>RSS demo</title>
<META http-equiv=content-type content="text/html; charset=GBK">
<META content="MSHTML 6.00.2462.0" name=GENERATOR></HEAD>
</head>
<body>
<a href="http://fusion.google.com/add?source=atgs&feedurl=http%3A//rss.sina.com.cn/news/marquee/ddt.xml"><img src="http://buttons.googlesyndication.com/fusion/add.gif" alt="Add to Google"></a><br />
<a target="_blank" title="订阅到鲜果 RSS阅读器" href="http://xianguo.com/subscribe?url=http%3A%2F%2Frss.sina.com.cn%2Fnews%2Fmarquee%2Fddt.xml"><img src="http://static.xgres.com/static/images/sub/sub_XianGuo_02.gif" border="0" alt="鲜果阅读器订阅图标" /></a><br />
<a href="http://mail.qq.com/cgi-bin/feed?u=http://rss.sina.com.cn/news/marquee/ddt.xml">订阅到QQ邮箱</a><br />
<a target="_blank" href="http://reader.youdao.com/b.do?keyfrom=bookmarklet&url=http%3A%2F%2Frss.sina.com.cn%2Fnews%2Fmarquee%2Fddt.xml"><img src="http://reader.youdao.com/images/feed-btn-4.gif" border="0" alt="订阅到有道阅读" /></a>
</body>
</html>
后面有找到了两个
5、订阅到MSN
只要把连接http://my.msn.com/addtomymsn.armx?id=rss&ut=http://rss.sina.com.cn/news/marquee/ddt.xml中的ut=后面的改为自己的RSS地址就可以了
6、订阅到雅虎
只要把连接http://add.my.yahoo.com/rss?url=http://rss.sina.com.cn/news/marquee/ddt.xml中的url=后面换成自己的RSS地址即可