基于java的RSS在线订阅demo

转载于:http://www.javaeye.com/topic/677073

 

由于项目的问题,需要有RSS订阅的概念,以前听说过这个技术,但是却没使用过。今天下午正好没事做,而公司又有这方面的需求,故从官网下了源代码,再结合网上一些前辈对这技术的经验,于是就动手做了起来。临近快下班时间,测试版本已经成功做出,并且能支持订阅和查看,现将RSS订阅这方面的Java版本资料给出,欢迎各位指教,一起探讨。由于网络上对RSS的解释不是很全,故这里给大家总结下,不好的地方请指正,大家一同进步。 
什么是RSS? 
  RSS是一种网页内容联合格式(web content sydication format)。 
  它的名字是Really Simple Syndication的缩写。 
  RSS是XML的一种。所有的RSS文档都遵循XML 1.0规范,该规范发布在W3C网站上。 
  RSS是站点用来和其他站点之间共享内容的一种简易方式(也叫聚合内容),通常被用于新闻和其他按顺序排列的网站,例如Blog。一段项目的介绍可能包含新闻的全部介绍,Blog post等等。 
上面简单提了下RSS,不明白的地方可以直接网上搜索百科全书,这方面有个详细的介绍。下面就直接上代码了。 

Java代码  复制代码
  1. /**    
  2.      * 根据链接地址得到数据    
  3.      * @param url RSS形式的xml文件    
  4.      * @throws IllegalArgumentException    
  5.      * @throws FeedException    
  6.      */     
  7.     public void parseXml(URL url) throws IllegalArgumentException, FeedException {         
  8.      
  9.         try {         
  10.             SyndFeedInput input = new SyndFeedInput();         
  11.             SyndFeed feed = null;         
  12.             URLConnection conn;         
  13.             conn = url.openConnection();         
  14.             String content_encoding = conn.getHeaderField("Content-Encoding");         
  15.                   
  16.             if (content_encoding != null && content_encoding.contains("gzip")) {         
  17.                 System.out.println("conent encoding is gzip");         
  18.                 GZIPInputStream gzin = new GZIPInputStream(conn         
  19.                         .getInputStream());         
  20.                 feed = input.build(new XmlReader(gzin));         
  21.             } else {         
  22.                 feed = input.build(new XmlReader(conn.getInputStream()));         
  23.             }         
  24.                   
  25.             List entries = feed.getEntries();//得到所有的标题<title></title>      
  26.             for(int i=0; i < entries.size(); i++) {      
  27.                 SyndEntry entry = (SyndEntry)entries.get(i);      
  28.                 System.out.println(entry.getTitle());      
  29.             }      
  30.             System.out.println("feed size:" + feed.getEntries().size());         
  31.               
  32.         } catch (IOException e) {         
  33.             e.printStackTrace();         
  34.         }         
  35.           
  36.     }       
  37. public void createXml() throws Exception {      
  38.         /* 根据Channel源码提供的英文,Channel对象有两个构造器,一个默认的无参构造器用于clone对象,一个是有参的    
  39.         * 我们自己指定的必须使用有参数的(因为我们需要许可证),指构造方法必须要创建一个type(版本),这个type不能随便写,必须要以rss_开头的版本号    
  40.         * Licensed under the Apache License, Version 2.0 (the "License");    
  41.         * 因为当前版本是2.0,所以就是rss_2.0,必须是rss_2.0否则会抛异常,该源码中写的已经很明白。    
  42.         */     
  43.        Channel channel = new Channel("rss_2.0");      
  44.        channel.setTitle("channel标题");//网站标题      
  45.         channel.setDescription("channel的描述");//网站描述      
  46.         channel.setLink("www.shlll.net");//网站主页链接      
  47.         channel.setEncoding("utf-8");//RSS文件编码      
  48.         channel.setLanguage("zh-cn");//RSS使用的语言      
  49.         channel.setTtl(5);//time to live的简写,在刷新前当前RSS在缓存中可以保存多长时间(分钟)      
  50.         channel.setCopyright("版权声明");//版权声明      
  51.         channel.setPubDate(new Date());//RSS发布时间      
  52.         List<Item> items = new ArrayList<Item>();//这个list对应rss中的item列表                
  53.         Item item = new Item();//新建Item对象,对应rss中的<item></item>      
  54.        item.setAuthor("hxliu");//对应<item>中的<author></author>      
  55.        item.setTitle("新闻标题");//对应<item>中的<title></title>      
  56.        item.setGuid(new Guid());//GUID=Globally Unique Identifier 为当前新闻指定一个全球唯一标示,这个不是必须的      
  57.         item.setPubDate(new Date());//这个<item>对应的发布时间      
  58.         item.setComments("注释");//代表<item>节点中的<comments></comments>      
  59.         //新建一个Description,它是Item的描述部分      
  60.         Description description = new Description();      
  61.        description.setValue("新闻主题");//<description>中的内容      
  62.         item.setDescription(description);//添加到item节点中      
  63.         items.add(item);//代表一个段落<item></item>,      
  64.         channel.setItems(items);      
  65.         //用WireFeedOutput对象输出rss文本      
  66.         WireFeedOutput out = new WireFeedOutput();      
  67.         try {      
  68.             System.out.println(out.outputString(channel));      
  69.         } catch (IllegalArgumentException e) {      
  70.             e.printStackTrace();      
  71.         } catch (FeedException e) {      
  72.             e.printStackTrace();      
  73.         }      
  74.      
  75. }    
[java]  view plain  copy
  1. <span style="font-size: medium;">/**   
  2.      * 根据链接地址得到数据   
  3.      * @param url RSS形式的xml文件   
  4.      * @throws IllegalArgumentException   
  5.      * @throws FeedException   
  6.      */    
  7.     public void parseXml(URL url) throws IllegalArgumentException, FeedException {        
  8.     
  9.         try {        
  10.             SyndFeedInput input = new SyndFeedInput();        
  11.             SyndFeed feed = null;        
  12.             URLConnection conn;        
  13.             conn = url.openConnection();        
  14.             String content_encoding = conn.getHeaderField("Content-Encoding");        
  15.                  
  16.             if (content_encoding != null && content_encoding.contains("gzip")) {        
  17.                 System.out.println("conent encoding is gzip");        
  18.                 GZIPInputStream gzin = new GZIPInputStream(conn        
  19.                         .getInputStream());        
  20.                 feed = input.build(new XmlReader(gzin));        
  21.             } else {        
  22.                 feed = input.build(new XmlReader(conn.getInputStream()));        
  23.             }        
  24.                  
  25.             List entries = feed.getEntries();//得到所有的标题<title></title>     
  26.             for(int i=0; i < entries.size(); i++) {     
  27.                 SyndEntry entry = (SyndEntry)entries.get(i);     
  28.                 System.out.println(entry.getTitle());     
  29.             }     
  30.             System.out.println("feed size:" + feed.getEntries().size());        
  31.              
  32.         } catch (IOException e) {        
  33.             e.printStackTrace();        
  34.         }        
  35.          
  36.     }      
  37. public void createXml() throws Exception {     
  38.         /* 根据Channel源码提供的英文,Channel对象有两个构造器,一个默认的无参构造器用于clone对象,一个是有参的   
  39.         * 我们自己指定的必须使用有参数的(因为我们需要许可证),指构造方法必须要创建一个type(版本),这个type不能随便写,必须要以rss_开头的版本号   
  40.         * Licensed under the Apache License, Version 2.0 (the "License");   
  41.         * 因为当前版本是2.0,所以就是rss_2.0,必须是rss_2.0否则会抛异常,该源码中写的已经很明白。   
  42.         */    
  43.        Channel channel = new Channel("rss_2.0");     
  44.        channel.setTitle("channel标题");//网站标题     
  45.         channel.setDescription("channel的描述");//网站描述     
  46.         channel.setLink("www.shlll.net");//网站主页链接     
  47.         channel.setEncoding("utf-8");//RSS文件编码     
  48.         channel.setLanguage("zh-cn");//RSS使用的语言     
  49.         channel.setTtl(5);//time to live的简写,在刷新前当前RSS在缓存中可以保存多长时间(分钟)     
  50.         channel.setCopyright("版权声明");//版权声明     
  51.         channel.setPubDate(new Date());//RSS发布时间     
  52.         List<Item> items = new ArrayList<Item>();//这个list对应rss中的item列表               
  53.         Item item = new Item();//新建Item对象,对应rss中的<item></item>     
  54.        item.setAuthor("hxliu");//对应<item>中的<author></author>     
  55.        item.setTitle("新闻标题");//对应<item>中的<title></title>     
  56.        item.setGuid(new Guid());//GUID=Globally Unique Identifier 为当前新闻指定一个全球唯一标示,这个不是必须的     
  57.         item.setPubDate(new Date());//这个<item>对应的发布时间     
  58.         item.setComments("注释");//代表<item>节点中的<comments></comments>     
  59.         //新建一个Description,它是Item的描述部分     
  60.         Description description = new Description();     
  61.        description.setValue("新闻主题");//<description>中的内容     
  62.         item.setDescription(description);//添加到item节点中     
  63.         items.add(item);//代表一个段落<item></item>,     
  64.         channel.setItems(items);     
  65.         //用WireFeedOutput对象输出rss文本     
  66.         WireFeedOutput out = new WireFeedOutput();     
  67.         try {     
  68.             System.out.println(out.outputString(channel));     
  69.         } catch (IllegalArgumentException e) {     
  70.             e.printStackTrace();     
  71.         } catch (FeedException e) {     
  72.             e.printStackTrace();     
  73.         }     
  74.     
  75. }    
  76.   
  77. </span>  


以上提供了2个方法,一个是解析xml的方法,一个是生成xml的方法,有了这2个方法不管你是订阅还是解析都可以游刃有余了,由于时间关系,我下午只做了java版本的订阅,没有和页面绑定,但是这个我相信后台xml的生成和截取已经成功,页面只需要一个链接地址就可以了。附件中是RSS订阅需要用到的2个jar包,一并提供给大家,供大家一起研究探讨,我看网上有人说有这方面的API文档,我找了半天都没找到,就直接从网上下了个rome的源代码,发现源代码比API还好用些,因为有人说API上说的和源代码上写的根本不相同。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值