Rome自动生成Rss

 工具类里用的Jar包为Rome.jar,JDom.jar,感兴趣的朋友可以自己下载,也可以从我的上篇文件里下载到。

存在两个POJO类:ChannelItem类和ChannelEItem类,ChanneEItem类继承自ChannelItem类,多一个Enclosure属性,用于存在流媒体文件的类里使用。

具体代码如下,ChannelItem类:

import java.util.Date;

/**
* 频道下的子信息 此类无流媒体播放文件
*
* @author jackZhang
*
*/
public class ChannelItem {
   private String title;// Rss文件中Item的标题
   private String link;// Rss文件中Item对应的连接
   private String description;// Item的描述
   private Date pubDate;// Item发布的时间
   private String author;// Item作者
   private String category;// Item所属的频道范畴

   public String getTitle() {
       return title;
   }

   public void setTitle(String title) {
       this.title = title;
   }

   public String getLink() {
       return link;
   }

   public void setLink(String link) {
       this.link = link;
   }

   public String getDescription() {
       return description;
   }

   public void setDescription(String description) {
       this.description = description;
   }

   public Date getPubDate() {
       return pubDate;
   }

   public void setPubDate(Date pubDate) {
       this.pubDate = pubDate;
   }

   public String getAuthor() {
       return author;
   }

   public void setAuthor(String author) {
       this.author = author;
   }

   public String getCategory() {
       return category;
   }

   public void setCategory(String category) {
       this.category = category;
   }

import java.util.Date;  /**  * 频道下的子信息  * 此类无流媒体播放文件  * @author jackZhang  *  */ public class ChannelItem{  private String title;//Rss文件中Item的标题  private String link;//Rss文件中Item对应的连接  private String description;//Item的描述  private Date pubDate;//Item发布的时间  private String author;//Item作者  private String category;//Item所属的频道范畴    public String getTitle() {   return title;  }  public void setTitle(String title) {   this.title = title;  }  public String getLink() {   return link;  }  public void setLink(String link) {   this.link = link;  }  public String getDescription() {   return description;  }  public void setDescription(String description) {   this.description = description;  }  public Date getPubDate() {   return pubDate;  }  public void setPubDate(Date pubDate) {   this.pubDate = pubDate;  }  public String getAuthor() {   return author;  }  public void setAuthor(String author) {   this.author = author;  }  public String getCategory() {   return category;  }  public void setCategory(String category) {   this.category = category;  } }



ChannelEItem类,具体代码:

/**
* 用于添加频道的子项 当存在流媒体播放文件时使用此类
*
* @author jackZhang
*
*/
public class ChannelEItem extends ChannelItem {
    private String enclosure;// 流媒体文件

    public String getEnclosure() {
        return enclosure;
    }

    public void setEnclosure(String enclosure) {
        this.enclosure = enclosure;
    }
}
/**  * 用于添加频道的子项  * 当存在流媒体播放文件时使用此类  *  * @author jackZhang  *  */ public class ChannelEItem extends ChannelItem{  private String enclosure;//流媒体文件   public String getEnclosure() {   return enclosure;  }   public void setEnclosure(String enclosure) {   this.enclosure = enclosure;  } }



以下是具体的Rss生成器封装类RssBuildFactory,具体代码:
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.sun.syndication.feed.synd.SyndCategory;
import com.sun.syndication.feed.synd.SyndCategoryImpl;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEnclosure;
import com.sun.syndication.feed.synd.SyndEnclosureImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.SyndFeedOutput;

/**
* 用于生成Rss文件
*
* @author jackZhang
*
*/
public class RssBuildFactory {
    private SyndFeed feed;
    @SuppressWarnings("unchecked")
    private List entries;
    private SyndEntry entry;

    @SuppressWarnings("unchecked")
    public RssBuildFactory() {
        feed = new SyndFeedImpl();
        feed.setFeedType("rss_2.0");
        entries = new ArrayList();
    }

    /**
    * 创建一个频道
    *
    * @param title
    *            频道标题
    * @param link
    *            频道对应的连接
    * @param description
    *            频道描述
    * @param language
    *            频道所用语言
    * @param pubDate
    *            频道发布时期
    * @param copyright
    *            版权所有
    * @throws Exception
    */
    public void buildChannel(String title, String link, String description,
            String language, Date pubDate, String copyright)
            throws RuntimeException {
        feed.setTitle(title);
        feed.setLink(link);
        feed.setDescription(description);
        feed.setLanguage(language);
        feed.setPublishedDate(pubDate);
        feed.setCopyright(copyright);
    }

    /**
    * 添加频道的子内容
    *
    * @param item
    *            <a href="mailto:%7B@link">{@link</a> ChannelItem}
    * @throws Exception
    */
    @SuppressWarnings("unchecked")
    public void buildItems(ChannelItem item) throws RuntimeException {
        entry = new SyndEntryImpl();
        // 设置新闻标题
        entry.setTitle(item.getTitle());
        // 设置新闻的连接地址
        entry.setLink(item.getLink());
        // 设置新闻简介
        SyndContent content = new SyndContentImpl();
        content.setType("text/plain");
        content.setValue(item.getDescription());
        entry.setDescription(content);
        // 设置发布时间
        entry.setPublishedDate(item.getPubDate());
        // 设置频道所属的范围
        SyndCategory cate = new SyndCategoryImpl();
        cate.setName(item.getCategory());
        List cateList = new ArrayList();
        cateList.add(cate);
        entry.setCategories(cateList);
        // 设置作者
        entry.setAuthor(item.getAuthor());
        // 将新闻项添加至数组中
        entries.add(entry);
    }

    /**
    * 添加频道的内容项
    *
    * @param item
    *            <a href="mailto:%7B@link">{@link</a> ChannelEItem}此类继承自ChannelItem类
    * @throws Exception
    */
    @SuppressWarnings("unchecked")
    public void buildItems(ChannelEItem item) throws RuntimeException {
        entry = new SyndEntryImpl();
        // 设置新闻标题
        entry.setTitle(item.getTitle());
        // 设置新闻的连接地址
        entry.setLink(item.getLink());
        // 设置新闻简介
        SyndContent content = new SyndContentImpl();
        content.setValue(item.getDescription());
        entry.setDescription(content);
        // 设置发布时间
        entry.setPublishedDate(item.getPubDate());
        // 设置频道所属的范围
        SyndCategory cate = new SyndCategoryImpl();
        cate.setName(item.getCategory());
        List cateList = new ArrayList();
        cateList.add(cate);
        entry.setCategories(cateList);
        // 设置作者
        entry.setAuthor(item.getAuthor());
        // 设置流媒体播放文件
        SyndEnclosure en = new SyndEnclosureImpl();
        en.setUrl(item.getEnclosure());
        List enList = new ArrayList();
        enList.add(en);
        entry.setEnclosures(enList);
        // 将新闻项添加至数组中
        entries.add(entry);
    }

    /**
    * 生成XML文件
    *
    * @param filePath
    *            文件保存路径和名称
    * @throws Exception
    */
    public void buildChannel(String filePath) throws Exception {
        feed.setEntries(entries);
        SyndFeedOutput output = new SyndFeedOutput();
        Writer writer;
        writer = new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8");
        output.output(feed, writer);
    }


下面是一个示例,用于测试生成Rss:

前提说明本人测试使用的Mysql数据库,数据库存在一个newsitem表用于储存Rss内容。

  1. public void testBuildObject() {  
  2.   try {  
  3.   
  4.  //建立数据库的连接  
  5.     DBConnection db = new DBConnection();  
  6.   
  7.  //查询Sql语句  
  8.     String querySql = "select * from newitem";  
  9.   
  10.    //getAllResult方法是一个查询方法,感兴趣的话也可以自己写  
  11.     ResultSet rs = db.getAllResult(querySql);  
  12.   
  13.  //建立Rss生成器工厂  
  14.     RssBuildFactory builder = new RssBuildFactory();  
  15.   
  16.    //循环遍历数据库记录生成Rss中的Item项  
  17.    while (rs.next()) {  
  18.      ChannelEItem item = new ChannelEItem();  
  19.      item.setTitle(rs.getString("title"));  
  20.      item.setLink(rs.getString("link"));  
  21.      item.setDescription(rs.getString("description"));  
  22.      item.setPubDate(rs.getDate("pubdate"));  
  23.      item.setCategory(rs.getString("category"));  
  24.      item.setAuthor(rs.getString("author"));  
  25.      item.setEnclosure(rs.getString("enclosure"));  
  26.      builder.buildItems(item);  
  27.     }  
  28.   
  29.    //建立Rss的Channel信息  
  30.     builder.buildChannel("Jack的测试", "<a href="http://www.fansgoo.com/" target="_blank">www.fansgoo.com</a>", "测试生成", "zh-cn",  
  31.      new Date(), "河南亿禧软件有限公司");  
  32.   
  33.  //设置Rss文件的生成路径  
  34.     builder.buildChannel("E://demo.xml");  
  35.    } catch (Exception e) {  
  36.     e.printStackTrace();  
  37.    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值