RSS文章订阅及生成RSS格式的xml
来源:互联网 发布:怎样做好一个软件 编辑:程序博客网 时间:2024/06/05 02:57
现在很多的新闻网站,博客网站等都提供了RSS订阅功能,我们可以很方便的进行RSS订阅,下面我使用rome-0.9.jar进行RSS订阅,具体代码如下:
如订阅网页新闻,先要得到该RSS订阅的地址(xml页面):http://news.163.com/special/00011K6L/rss_gn.xml;
URL feedUrl = new URL("http://news.163.com/special/00011K6L/rss_gn.xml"); SyndFeedInput input = new SyndFeedInput(); SyndFeed feed = input.build(new XmlReader(feedUrl)); System.out.println(feed); //从feed中得到entry List list = feed.getEntries(); for (int i=0; i< list.size(); i++) { SyndEntry entry = (SyndEntry)list.get(i); System.out.println("链接为 = "+entry.getLink()); System.out.println("标题为 = "+entry.getTitle()); System.out.println("时间为 = "+entry.getPublishedDate()); System.out.println("内容为 = "+entry.getDescription().getValue()); System.out.println("Categories为 = "+entry.getCategories().get(0)); System.out.println("=============================================================================="); }
非常方便,同样使用该包来创建RSS格式的xml进行订阅操作也是非常方便的,下面进行示例:
package djn.test.rss.rome;import com.sun.syndication.feed.rss.*;import com.sun.syndication.io.FeedException;import com.sun.syndication.io.WireFeedOutput;import java.util.*;public class RssFeedFactory { public static void main(String [] args){ //新建Channel对象,对应rss中的<channel></channel> /* Channel对象有两个构造器,一个默认的无参构造器用于clone对象, * 平时创建Channel对象时只能使用有参构造器Channel(String type) * 这个参数type很讲究,起初我随便填写了一些字符串,都抛出异常,非法的type * 后来逼急了,上网把rome源码搞下来,才搞明白type得是"rss_x.x"这样的 * rome的文档里也没有写明,浪费了不少时间研究这个type究竟应该是什么。 */ Channel channel = new Channel("rss_2.0"); channel.setTitle("Test RSS channel's title"); channel.setDescription("channel的描述"); channel.setLink("http://hi.baidu.com/openj/rss"); channel.setEncoding("GBK"); //这个list对应rss中的item列表 List items = new ArrayList(); //新建Item对象,对应rss中的<item></item> Item item = new Item(); item.setAuthor("item author jnduan"); item.setTitle("item title"); //新建一个Description,它是Item的描述部分 Description description = new Description(); description.setType("item description type"); description.setValue("item description value"); item.setDescription(description); items.add(item); channel.setItems(items); //用WireFeedOutput对象输出rss文本 WireFeedOutput out = new WireFeedOutput(); try { System.out.println(out.outputString(channel)); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (FeedException e) { e.printStackTrace(); } }}
生成的RSS格式的xml
<?xml version="1.0" encoding="GBK"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
<channel>
<title>Test RSS channel's title</title>
<link>http://hi.baidu.com/openj/rss</link>
<description>channel的描述</description>
<item>
<title>item title</title>
<description>item description value</description>
<author>item author jnduan</author>
</item>
</channel>
</rss>
- RSS文章订阅及生成RSS格式的xml
- RSS订阅的xml格式
- xml实现rss订阅
- XML--做rss订阅
- Rss订阅其它网站的文章
- php动态生成xml格式的rss文件
- 我订阅的 rss
- phpcms rss订阅格式修改
- 如何用ASP生成XML数据文档(RSS订阅)
- Rss订阅
- RSS订阅
- RSS订阅
- RSS订阅
- RSS订阅
- RSS订阅
- rss订阅
- Rss订阅
- rss订阅
- 根据status信息对MySQL服务器进行优化
- 设计模式DP(3)抽象工厂
- 【小蒙淘金】3.5午夜金评-晚间快速波动零点行情如何把握?
- LIB和DLL的区别与使用
- Rss标准格式
- RSS文章订阅及生成RSS格式的xml
- effctive c++ 学习
- VMWare安装64位CentOS6.3出现Unable to boot - please use a kernel appropriate for your CPU.解决方法
- 揭露卖劣质U盘黑心商家的利器
- 如何安装memcached
- 简单ftp实现(get和put)
- ViewConfiguration的简单介绍
- VMware Workstation虚拟机与主机共享文件
- red hat 5.4上安装基于vsftpd的ftp服务器