Romeを使用したRSSの生成

3936 ワード

Romeはdev.javaです.Netの次のオープンソースプロジェクトは、「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の各モジュールをよくパッケージ化し、機能が強い.
RomeでRssを生成するのは簡単です.Romeのクラスライブラリを見るのに少し時間がかかり、Romeが持っている参考例を見るのに少し時間がかかります.
まずromeをjarとjdom.JArはclasspathに参加します.
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.io.SyndFeedOutput;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class RssWriter {

 public boolean ok = false;
 //public String feedType = "rss_2.0";
 private static final String DATE_FORMAT = "yyyy-MM-dd";
 DateFormat dateParser = new SimpleDateFormat(DATE_FORMAT);
 
 private SyndFeed feed = new SyndFeedImpl();

 //        ,       
 public RssWriter(String host,String title,String link,String feedType,String des){
  feed.setFeedType(feedType);
        feed.setTitle(title);
        feed.setLink(link);
        feed.setAuthor(host);
        feed.setDescription(des);
 }
 
 public RssWriter(String host,String title,String des,String link,String feedType,String Encoding){
        feed.setFeedType(feedType);
        feed.setTitle(title);
        feed.setLink(link);
        feed.setDescription(des);
        feed.setAuthor(host);
        feed.setEncoding(Encoding);
 }
 
 /**
  *   map     bean         ,           feed  
  */
 public void WriteFeed(String fileName,List arr){
   try{
  List entries = new ArrayList();
        SyndEntry entry;
        SyndContent description;
     for(int i=0;i<arr.size();i++){   
        entry = new SyndEntryImpl();
        description = new SyndContentImpl();
        HashMap hm = (HashMap)arr.get(i);

        entry.setTitle((String) hm.get("title"));
        entry.setAuthor((String) hm.get("author"));
        entry.setUpdatedDate(new Date());
        entry.setLink((String) hm.get("link"));
        entry.setPublishedDate((Date) hm.get("pubdate"));
        description.setType((String) hm.get("type"));
        description.setValue((String) hm.get("content"));
        entry.setDescription(description);
        
        entries.add(entry);
     }
        feed.setEntries(entries);
        Writer writer = new FileWriter(fileName);
        SyndFeedOutput output = new SyndFeedOutput();
        output.output(feed,writer);
        writer.close();
        ok = true;
    }
    catch (Exception ex) {
        ex.printStackTrace();
        System.out.println("ERROR: "+ex.getMessage());
    }
if (!ok) {
    System.out.println();
     System.out.println("---------create the rss file fail.----------");
    System.out.println();
   }
  }
  
}

呼び出し例:
RssWriter rw = new RssWriter("wzw","wo zai zhe li","hao123.com","rss_2.0","zmhsya");
  
  List list = a.getList();
  List arr = new LinkedList();
  Map hashmap = null;
  for(int i=0;i<list.size();i++){
     Article article = (Article)list.get(i);
     hashmap = new HashMap();
     hashmap.put("title",article.getTitle());
     hashmap.put("author",article.getUserAccountByUserId().getUserName());
     hashmap.put("link",http://www.youziblog.com);
     hashmap.put("pubdate",new Date());
     hashmap.put("type","text/html");
     hashmap.put("content",article.getContent());
     arr.add(hashmap);
  }
  rw.WriteFeed(request.getRealPath("/") + "/rss/articles.rss",arr);

これによりwebroot/rssディレクトリの下でarticlesが生成する.rssだ