基於java的RSS線上訂閱demo

maqianmaqian發表於2010-10-09

/**  
     * 根據連結地址得到資料  
     * @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();   
        }   
 
}

http://yueding920.blog.163.com/blog/static/352508902010521104722543/

 

相關文章