使用Rome 生成RSS

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


package com.cnfilm.utils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import com.cnfilm.web.film.Film; import com.sun.syndication.feed.rss.Channel; import com.sun.syndication.feed.rss.Description; import com.sun.syndication.feed.rss.Item; import com.sun.syndication.io.FeedException; import com.sun.syndication.io.WireFeedOutput; /** * 文件名:RssUtils.java 网站RSS生成 * 版本信息:V1.0 * 日期:2013-06-18 * Copyright BDVCD Corporation 2013 * 版权所有 http://www.bdvcd.com */ public class RssUtils { public static String getRssString(List filmList,HashMap map){ Channel channel = new Channel("rss_2.0"); channel.setTitle(map.get("title")); channel.setDescription(map.get("description")); channel.setLink("http://www.bdvcd.com/"); channel.setEncoding("UTF-8"); /**这个list对应rss中的item列表**/ List items = new ArrayList(); /**新建Item对象,对应rss中的**/ Item item = null; for(Film film:filmList){ item = new Item(); item.setAuthor(film.getStr("starring")); item.setLink("http://www.bdvcd.com/"+film.getStr("curl")+"/"+film.getStr("url")+".html"); item.setPubDate(DateUtils.parse(film.getStr("addtime"))); item.setTitle(film.getStr("fname")); Description description = new Description(); description.setValue(film.getStr("content")); item.setDescription(description); items.add(item); } channel.setItems(items); /**用WireFeedOutput对象输出rss文本**/ WireFeedOutput out = new WireFeedOutput(); String rssString = ""; try { rssString = out.outputString(channel); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (FeedException e) { e.printStackTrace(); } return rssString; } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值