使用rome生成rss格式的xml.docx_第1页
使用rome生成rss格式的xml.docx_第2页
使用rome生成rss格式的xml.docx_第3页
使用rome生成rss格式的xml.docx_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

项目文档里推荐使用rome或rsslib4j来生成rss格式的xml。rome是下的一个开源的项目,是一个“解析、创建、发布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.rssClasses Category Channel Cloud Content Description Enclosure Guid Image Item Source TextInput com.sun.syndication.io 这个包里的类负责解析,输出等io操作com.sun.syndication.ioInterfaces DelegatingModuleGeneratorDelegatingModuleParserModuleGeneratorModuleParserWireFeedGeneratorWireFeedParserClasses SAXBuilderSyndFeedInputSyndFeedOutputWireFeedInputWireFeedOutputXmlReaderExceptions FeedExceptionParsingFeedExceptionXmlReaderException下面是一个我学习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对象有两个构造器,一个默认的无参构造器用于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 channels title); channel.setDescription(channel的描述); channel.setLink(/openj/rss); channel.setEncoding(GBK); /这个list对应rss中的item列表 List items = new ArrayList(); /新建Item对象,对应rss中的 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(); 运行结果如下: Test RSS channels title /openj/rss channel的描述 item

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论