您的位置:首页 > 其它

使用rome创建RSS格式的xml

2009-03-15 21:52 357 查看
项目文档里推荐使用romersslib4j来生成rss格式的xml。

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中的各个模块都进行了很好的封装,“很好很强大”。

rsslib4j是gnu下的一个开源项目,“Rsslib4j is a set of Java API to parse and retrive information from a RSS Feed. It supports RSS version 0.9x ,1.0 and 2.0 specification with Doublin Core and Syndication namespace”,懒得翻译了,从名字就可以看出来,它只局限于rss相关的功能,相当于是被“阉割”了的rome吧。

分别下载了两个项目的文件,看了看文档和API,里面的类基本上都差不多,也不知道用哪个好些。考虑到rome比较大而全,因此考虑使用rome来构建项目的rss生成服务。

使用rome需要把rome-0.9.jar加入classpath,除此之外并没有其他的依赖。

一般的,创建一个rss格式的xml文档需要用到如下包:

com.sun.syndication.feed.rss 顾名思义,这个包里是封装了rss各个部分的对象,如channel,item等等

com.sun.syndication.feed.rss
Classes

Category
Channel
Cloud
Content
Description
Enclosure
Guid
Image
Item
Source
TextInput

com.sun.syndication.io 这个包里的类负责解析,输出等io操作

com.sun.syndication.io

Interfaces
DelegatingModuleGenerator
DelegatingModuleParser
ModuleGenerator
ModuleParser
WireFeedGenerator
WireFeedParser
Classes
SAXBuilder
SyndFeedInput
SyndFeedOutput
WireFeedInput
WireFeedOutput
XmlReader
Exceptions
FeedException
ParsingFeedException
XmlReaderException

下面是一个我学习rome过程中写的一个小例子:

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();
}
}
}

运行结果如下:

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

总体来说,rome以及rsslib4j的用法很简单,只要弄明白rss文本的结构就很容易写出生成rss文本的程序,下一步,计划如何编写一个通用的服务,很好的完成业务上的需求。

相关文章:
《RSS格式规范》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: