RSS文章订阅及生成RSS格式的xml

现在很多的新闻网站,博客网站等都提供了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>

 

 

 

希望多多交流,多多关注,共同成就梦想
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值