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了.