您的位置:首页 > 编程语言 > Java开发

基于java的RSS在线订阅demo

2016-09-02 11:13 330 查看
转载于: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代码 



/**    

     * 根据链接地址得到数据    

     * @param url RSS形式的xml文件    

     * @throws IllegalArgumentException    

     * @throws FeedException    

     */  
  

    public void parseXml(URL url) throws IllegalArgumentException, FeedException {      
  

     

        try {      
  

            SyndFeedInput input = new SyndFeedInput();      
  

            SyndFeed feed = null;      
  

            URLConnection conn;         

            conn = url.openConnection();         

            String content_encoding = conn.getHeaderField("Content-Encoding");      
  

                  

            if (content_encoding != null && content_encoding.contains("gzip")) {      
  

                System.out.println("conent encoding is gzip");      
  

                GZIPInputStream gzin = new GZIPInputStream(conn      
  

                        .getInputStream());         

                feed = input.build(new XmlReader(gzin));      
  

            } else {      
  

                feed = input.build(new XmlReader(conn.getInputStream()));      
  

            }         

                  

            List entries = feed.getEntries();//得到所有的标题<title></title>      

            for(int i=0; i < entries.size(); i++) {   
  

                SyndEntry entry = (SyndEntry)entries.get(i);      

                System.out.println(entry.getTitle());      

            }      

            System.out.println("feed size:" + feed.getEntries().size());      
  

              

        } catch (IOException e) {      
  

            e.printStackTrace();         

        }         

          

    }       

public void createXml() throws Exception {   
  

        /* 根据Channel源码提供的英文,Channel对象有两个构造器,一个默认的无参构造器用于clone对象,一个是有参的    

        * 我们自己指定的必须使用有参数的(因为我们需要许可证),指构造方法必须要创建一个type(版本),这个type不能随便写,必须要以rss_开头的版本号    

        * Licensed under the Apache License, Version 2.0 (the "License");    

        * 因为当前版本是2.0,所以就是rss_2.0,必须是rss_2.0否则会抛异常,该源码中写的已经很明白。    

        */  
  

       Channel channel = new Channel("rss_2.0");   
  

       channel.setTitle("channel标题");//网站标题      

        channel.setDescription("channel的描述");//网站描述      

        channel.setLink("www.shlll.net");//网站主页链接      

        channel.setEncoding("utf-8");//RSS文件编码      

        channel.setLanguage("zh-cn");//RSS使用的语言      

        channel.setTtl(5);//time to live的简写,在刷新前当前RSS在缓存中可以保存多长时间(分钟)      

        channel.setCopyright("版权声明");//版权声明      

        channel.setPubDate(new Date());//RSS发布时间      

        List<Item> items = new ArrayList<Item>();//这个list对应rss中的item列表                

        Item item = new Item();//新建Item对象,对应rss中的<item></item>      

       item.setAuthor("hxliu");//对应<item>中的<author></author>      

       item.setTitle("新闻标题");//对应<item>中的<title></title>      

       item.setGuid(new Guid());//GUID=Globally Unique Identifier 为当前新闻指定一个全球唯一标示,这个不是必须的      

        item.setPubDate(new Date());//这个<item>对应的发布时间      

        item.setComments("注释");//代表<item>节点中的<comments></comments>      

        //新建一个Description,它是Item的描述部分      

        Description description = new Description();   
  

       description.setValue("新闻主题");//<description>中的内容      

        item.setDescription(description);//添加到item节点中      

        items.add(item);//代表一个段落<item></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();      

        }      

     

}    

[java] view
plain copy

<span style="font-size: medium;">/**   

     * 根据链接地址得到数据   

     * @param url RSS形式的xml文件   

     * @throws IllegalArgumentException   

     * @throws FeedException   

     */    

    public void parseXml(URL url) throws IllegalArgumentException, FeedException {        

    

        try {        

            SyndFeedInput input = new SyndFeedInput();        

            SyndFeed feed = null;        

            URLConnection conn;        

            conn = url.openConnection();        

            String content_encoding = conn.getHeaderField("Content-Encoding");        

                 

            if (content_encoding != null && content_encoding.contains("gzip")) {        

                System.out.println("conent encoding is gzip");        

                GZIPInputStream gzin = new GZIPInputStream(conn        

                        .getInputStream());        

                feed = input.build(new XmlReader(gzin));        

            } else {        

                feed = input.build(new XmlReader(conn.getInputStream()));        

            }        

                 

            List entries = feed.getEntries();//得到所有的标题<title></title>     

            for(int i=0; i < entries.size(); i++) {     

                SyndEntry entry = (SyndEntry)entries.get(i);     

                System.out.println(entry.getTitle());     

            }     

            System.out.println("feed size:" + feed.getEntries().size());        

             

        } catch (IOException e) {        

            e.printStackTrace();        

        }        

         

    }      

public void createXml() throws Exception {     

        /* 根据Channel源码提供的英文,Channel对象有两个构造器,一个默认的无参构造器用于clone对象,一个是有参的   

        * 我们自己指定的必须使用有参数的(因为我们需要许可证),指构造方法必须要创建一个type(版本),这个type不能随便写,必须要以rss_开头的版本号   

        * Licensed under the Apache License, Version 2.0 (the "License");   

        * 因为当前版本是2.0,所以就是rss_2.0,必须是rss_2.0否则会抛异常,该源码中写的已经很明白。   

        */    

       Channel channel = new Channel("rss_2.0");     

       channel.setTitle("channel标题");//网站标题     

        channel.setDescription("channel的描述");//网站描述     

        channel.setLink("www.shlll.net");//网站主页链接     

        channel.setEncoding("utf-8");//RSS文件编码     

        channel.setLanguage("zh-cn");//RSS使用的语言     

        channel.setTtl(5);//time to live的简写,在刷新前当前RSS在缓存中可以保存多长时间(分钟)     

        channel.setCopyright("版权声明");//版权声明     

        channel.setPubDate(new Date());//RSS发布时间     

        List<Item> items = new ArrayList<Item>();//这个list对应rss中的item列表               

        Item item = new Item();//新建Item对象,对应rss中的<item></item>     

       item.setAuthor("hxliu");//对应<item>中的<author></author>     

       item.setTitle("新闻标题");//对应<item>中的<title></title>     

       item.setGuid(new Guid());//GUID=Globally Unique Identifier 为当前新闻指定一个全球唯一标示,这个不是必须的     

        item.setPubDate(new Date());//这个<item>对应的发布时间     

        item.setComments("注释");//代表<item>节点中的<comments></comments>     

        //新建一个Description,它是Item的描述部分     

        Description description = new Description();     

       description.setValue("新闻主题");//<description>中的内容     

        item.setDescription(description);//添加到item节点中     

        items.add(item);//代表一个段落<item></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();     

        }     

    

}    

  

</span>  

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

jdom.jar (149.2 KB)
下载次数: 539
rome-1.0.jar (214.5 KB)
下载次数: 553
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  rss