Java生成Rss文件内容

方法:

package com.ninemax.application.rss;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ninemax.application.rss.model.ChannelEItem;
import com.ninemax.application.rss.model.ChannelItem;
import com.ninemax.application.rss.utils.DBConnection;
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 Darker
 * 
 */
public class RssBuildFactory {
	private SyndFeed feed;
	@SuppressWarnings("all")
	private List entries;
	private SyndEntry entry;

	@SuppressWarnings("all")
	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("all")
	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<SyndCategory> 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("all")
	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 = new OutputStreamWriter(new FileOutputStream(filePath),
				"UTF-8");
		output.output(feed, writer);
	}

	public void testBuildObject() {
		try {
			// 建立数据库的连接
			DBConnection db = new DBConnection();
			db.MysqlConnection();
			// 查询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("D:\\rss\\rss.xml");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

main方法:

package com.ninemax.application.rss;

/**
 * 生成rss文件
 * 
 * @author Darker
 * 
 */
public class RssFileByRome {

	public static void main(String[] args) throws Exception {

		RssBuildFactory rss = new RssBuildFactory();

		rss.testBuildObject();
		
		System.out.println("rss.xml  success");
	}
}

生成Rss文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Jack的测试</title>
    <link>[url=http://www.51msh.com/]www.fansgoo.com[/url]</link>
    <description>测试生成</description>
    <language>zh-cn</language>
    <copyright>名声网</copyright>
    <pubDate>Fri, 18 Mar 2016 07:18:11 GMT</pubDate>
    <dc:date>2016-03-18T07:18:11Z</dc:date>
    <dc:language>zh-cn</dc:language>
    <dc:rights>名声网</dc:rights>
    <item>
      <title>RssTest</title>
      <link>http://www.baidu.com</link>
      <description>RssTest</description>
      <enclosure url="https://www.facebook.com/" />
      <category>https://www.facebook.com/</category>
      <pubDate>Tue, 15 Mar 2016 16:00:00 GMT</pubDate>
      <guid>http://www.baidu.com</guid>
      <dc:creator>darker</dc:creator>
      <dc:date>2016-03-15T16:00:00Z</dc:date>
    </item>
  </channel>
</rss>


转载于:https://my.oschina.net/Tsher2015/blog/639966

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值