论坛首页 Java企业应用论坛

自动生成Rss

浏览 6562 次
锁定老帖子 主题:自动生成Rss
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-07-14   最后修改:2009-02-09
前几天写了个解析Rss文件的一个例子,这几天写了个基于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;
 }
}


ChannelEItem类,具体代码:

/**
 * 用于添加频道的子项
 * 当存在流媒体播放文件时使用此类
 *
 * @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;
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内容。

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文件,方面于使用。
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics