自动生成Rss

转载于:http://www.javaeye.com/topic/214644

 

前几天写了个解析Rss文件的一个例子,这几天写了个基于Rome生成Rss文件的工具类,发布上来共享下。希望对大家有所帮助。

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

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

  具体代码如下,ChannelItem类:

Java代码 复制代码
  1. import java.util.Date;   
  2.   
  3. /**  
  4.  * 频道下的子信息  
  5.  * 此类无流媒体播放文件  
  6.  * @author jackZhang  
  7.  *  
  8.  */  
  9. public class ChannelItem{   
  10.  private String title;//Rss文件中Item的标题   
  11.  private String link;//Rss文件中Item对应的连接   
  12.  private String description;//Item的描述   
  13.  private Date pubDate;//Item发布的时间   
  14.  private String author;//Item作者   
  15.  private String category;//Item所属的频道范畴   
  16.     
  17.  public String getTitle() {   
  18.   return title;   
  19.  }   
  20.  public void setTitle(String title) {   
  21.   this.title = title;   
  22.  }   
  23.  public String getLink() {   
  24.   return link;   
  25.  }   
  26.  public void setLink(String link) {   
  27.   this.link = link;   
  28.  }   
  29.  public String getDescription() {   
  30.   return description;   
  31.  }   
  32.  public void setDescription(String description) {   
  33.   this.description = description;   
  34.  }   
  35.  public Date getPubDate() {   
  36.   return pubDate;   
  37.  }   
  38.  public void setPubDate(Date pubDate) {   
  39.   this.pubDate = pubDate;   
  40.  }   
  41.  public String getAuthor() {   
  42.   return author;   
  43.  }   
  44.  public void setAuthor(String author) {   
  45.   this.author = author;   
  46.  }   
  47.  public String getCategory() {   
  48.   return category;   
  49.  }   
  50.  public void setCategory(String category) {   
  51.   this.category = category;   
  52.  }   
  53. }  
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类,具体代码:

Java代码 复制代码
  1. /**  
  2.  * 用于添加频道的子项  
  3.  * 当存在流媒体播放文件时使用此类  
  4.  *  
  5.  * @author jackZhang  
  6.  *  
  7.  */  
  8. public class ChannelEItem extends ChannelItem{   
  9.  private String enclosure;//流媒体文件   
  10.   
  11.  public String getEnclosure() {   
  12.   return enclosure;   
  13.  }   
  14.   
  15.  public void setEnclosure(String enclosure) {   
  16.   this.enclosure = enclosure;   
  17.  }   
  18. }  
/**
 * 用于添加频道的子项
 * 当存在流媒体播放文件时使用此类
 *
 * @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,具体代码:

Java代码 复制代码
  1. import java.io.FileOutputStream;   
  2. import java.io.OutputStreamWriter;   
  3. import java.io.Writer;   
  4. import java.util.ArrayList;   
  5. import java.util.Date;   
  6. import java.util.List;   
  7.   
  8. import com.sun.syndication.feed.synd.SyndCategory;   
  9. import com.sun.syndication.feed.synd.SyndCategoryImpl;   
  10. import com.sun.syndication.feed.synd.SyndContent;   
  11. import com.sun.syndication.feed.synd.SyndContentImpl;   
  12. import com.sun.syndication.feed.synd.SyndEnclosure;   
  13. import com.sun.syndication.feed.synd.SyndEnclosureImpl;   
  14. import com.sun.syndication.feed.synd.SyndEntry;   
  15. import com.sun.syndication.feed.synd.SyndEntryImpl;   
  16. import com.sun.syndication.feed.synd.SyndFeed;   
  17. import com.sun.syndication.feed.synd.SyndFeedImpl;   
  18. import com.sun.syndication.io.SyndFeedOutput;   
  19. import com.yx.xml.bo.ChannelEItem;   
  20. import com.yx.xml.bo.ChannelItem;   
  21.   
  22. /**  
  23.  * 用于生成Rss文件  
  24.  * @author jackZhang  
  25.  *  
  26.  */  
  27. public class RssBuildFactory {   
  28.  private SyndFeed feed;   
  29.  @SuppressWarnings("unchecked")   
  30.  private List entries;   
  31.  private SyndEntry entry ;   
  32.  @SuppressWarnings("unchecked")   
  33.  public RssBuildFactory(){   
  34.   feed = new SyndFeedImpl();   
  35.   feed.setFeedType("rss_2.0");   
  36.   entries = new ArrayList();   
  37.  }   
  38.  /**  
  39.   * 创建一个频道  
  40.   * @param title 频道标题  
  41.   * @param link 频道对应的连接  
  42.   * @param description 频道描述  
  43.   * @param language 频道所用语言  
  44.   * @param pubDate 频道发布时期  
  45.   * @param copyright 版权所有  
  46.   * @throws Exception  
  47.   */  
  48.  public void buildChannel(String title,String link,String description,String language,Date pubDate,String copyright) throws RuntimeException {   
  49.   feed.setTitle(title);   
  50.   feed.setLink(link);   
  51.   feed.setDescription(description);   
  52.   feed.setLanguage(language);   
  53.   feed.setPublishedDate(pubDate);   
  54.   feed.setCopyright(copyright);   
  55.  }   
  56.     
  57.  /**  
  58.   * 添加频道的子内容  
  59.   * @param item [email={@link]{@link[/email] ChannelItem}  
  60.   * @throws Exception  
  61.   */  
  62.  @SuppressWarnings("unchecked")   
  63.  public void buildItems(ChannelItem item) throws RuntimeException {   
  64.   entry = new SyndEntryImpl();   
  65.   //设置新闻标题   
  66.   entry.setTitle(item.getTitle());   
  67.   //设置新闻的连接地址   
  68.   entry.setLink(item.getLink());   
  69.   //设置新闻简介   
  70.   SyndContent content = new SyndContentImpl();   
  71.   content.setType("text/plain");   
  72.   content.setValue(item.getDescription());   
  73.   entry.setDescription(content);   
  74.   //设置发布时间   
  75.   entry.setPublishedDate(item.getPubDate());   
  76.   //设置频道所属的范围   
  77.   SyndCategory cate = new SyndCategoryImpl();   
  78.   cate.setName(item.getCategory());   
  79.   List cateList = new ArrayList();   
  80.   cateList.add(cate);   
  81.   entry.setCategories(cateList);   
  82.   //设置作者   
  83.   entry.setAuthor(item.getAuthor());   
  84.   //将新闻项添加至数组中   
  85.   entries.add(entry);   
  86.  }   
  87.     
  88.  /**  
  89.   * 添加频道的内容项  
  90.   * @param item [email={@link]{@link[/email] ChannelEItem}此类继承自ChannelItem类  
  91.   * @throws Exception  
  92.   */  
  93.  @SuppressWarnings("unchecked")   
  94.  public void buildItems(ChannelEItem item) throws RuntimeException {   
  95.   entry = new SyndEntryImpl();   
  96.   //设置新闻标题   
  97.   entry.setTitle(item.getTitle());   
  98.   //设置新闻的连接地址   
  99.   entry.setLink(item.getLink());   
  100.   //设置新闻简介   
  101.   SyndContent content = new SyndContentImpl();   
  102.   content.setValue(item.getDescription());   
  103.   entry.setDescription(content);   
  104.   //设置发布时间   
  105.   entry.setPublishedDate(item.getPubDate());   
  106.   //设置频道所属的范围   
  107.   SyndCategory cate = new SyndCategoryImpl();   
  108.   cate.setName(item.getCategory());   
  109.   List cateList = new ArrayList();   
  110.   cateList.add(cate);   
  111.   entry.setCategories(cateList);   
  112.   //设置作者   
  113.   entry.setAuthor(item.getAuthor());   
  114.   //设置流媒体播放文件   
  115.   SyndEnclosure en = new SyndEnclosureImpl();   
  116.   en.setUrl(item.getEnclosure());   
  117.   List enList = new ArrayList();   
  118.   enList.add(en);   
  119.   entry.setEnclosures(enList);   
  120.   //将新闻项添加至数组中   
  121.   entries.add(entry);   
  122.  }   
  123.     
  124.  /**  
  125.   * 生成XML文件  
  126.   * @param filePath 文件保存路径和名称  
  127.   * @throws Exception  
  128.   */  
  129.  public void buildChannel(String filePath) throws Exception {   
  130.   feed.setEntries(entries);   
  131.   SyndFeedOutput output = new SyndFeedOutput();   
  132.   Writer writer;    
  133.   writer = new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8");   
  134.   output.output(feed, writer);     
  135.  }   
  136. }  
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;
import com.yx.xml.bo.ChannelEItem;
import com.yx.xml.bo.ChannelItem;

/**
 * 用于生成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 [email={@link]{@link[/email] 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 [email={@link]{@link[/email] 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内容。

Java代码 复制代码
  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的测试""[url=http://www.51msh.com/]www.fansgoo.com[/url]""测试生成""zh-cn",   
  31.      new Date(), "名声网");   
  32.   
  33.  //设置Rss文件的生成路径   
  34.    builder.buildChannel("E://demo.xml");   
  35.   } catch (Exception e) {   
  36.    e.printStackTrace();   
  37.   }   
  38.  }  
public void testBuildObject() {
  try {

 //建立数据库的连接
   DBConnection db = new DBConnection();

 //查询Sql语句
   String querySql = "select * from newitem";

   //getAllResult方法是一个查询方法,感兴趣的话也可以自己写
   ResultSet rs = db.getAllResult(querySql);

 //建立Rss生成器工厂
   RssBuildFactory builder = new RssBuildFactory();

   //循环遍历数据库记录生成Rss中的Item项
   while (rs.next()) {
    ChannelEItem item = new ChannelEItem();
    item.setTitle(rs.getString("title"));
    item.setLink(rs.getString("link"));
    item.setDescription(rs.getString("description"));
    item.setPubDate(rs.getDate("pubdate"));
    item.setCategory(rs.getString("category"));
    item.setAuthor(rs.getString("author"));
    item.setEnclosure(rs.getString("enclosure"));
    builder.buildItems(item);
   }

   //建立Rss的Channel信息
   builder.buildChannel("Jack的测试", "[url=http://www.51msh.com/]www.fansgoo.com[/url]", "测试生成", "zh-cn",
     new Date(), "名声网");

 //设置Rss文件的生成路径
   builder.buildChannel("E://demo.xml");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }



感兴趣的朋友也可以直接下载本人提供的上面类的Jar文件,方面于使用。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值