自己去下载jar包,rsslib4j-0.2.jar,rome-0.8.jar,jdom.jar。
表newitem如下(自己参考着建):
CREATE TABLE `newitem` (
`id` varchar(20) NOT NULL,
`title` varchar(30) default NULL,
`link` varchar(100) default NULL,
`description` varchar(100) default NULL,
`pubdate` date default NULL,
`category` varchar(30) default NULL,
`author` varchar(30) default NULL,
`enclosure` varchar(50) default NULL,
PRIMARY KEY (`id`)
)
下面是几个要用到类
import java.util.Date;
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;
}
}
public class ChannelEItem extends ChannelItem {
private String enclosure;// 流媒体文件
public String getEnclosure() {
return enclosure;
}
public void setEnclosure(String enclosure) {
this.enclosure = enclosure;
}
}
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;
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
* {@link 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
* {@link 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);
}
}
public class DBConnection {
public static void main(String[] args) {
DBConnection db = new DBConnection();
Connection conn = db.getConnection();
String sql = "select * from newitem";
ResultSet rs = db.getResultSet(sql, conn);
try {
while (rs.next()) {
String title = rs.getString("title");
System.out.println(title);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
// 建立到MySQL的连接
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/ding", "root", "ding");
} catch (Exception e) {
System.out.println("数据库连接异常!");
e.printStackTrace();
}
return conn;
}
public ResultSet getResultSet(String sql, Connection conn) {
ResultSet rs = null;
Statement stmt = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
}
最后这个是测试类
public class TestMain {
/**
* @Description 方法实现功能描述
* @param args
* void
* @throws 抛出异常说明
*/
public static void main(String[] args) {
new TestMain().testBuildObject();
}
public void testBuildObject() {
try {
// 建立数据库的连接
DBConnection db = new DBConnection();
// 查询Sql语句
String querySql = "select * from newitem";
Connection conn = db.getConnection();
ResultSet rs = db.getResultSet(querySql, conn);
// 建立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("laoding的测试",
"http://www.blogjava.net/laoding/", "测试生成", "zh-cn",
new Date(), "老丁");
// 设置Rss文件的生成路径
builder.buildChannel("demo.xml");
System.out.println("create rss xml success!!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
执行这个测试类,控制台输出:
create rss xml success!!
表示成功,生成的demo.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<title>laoding的测试</title>
<link>http://www.blogjava.net/laoding/</link>
<description>测试生成</description>
<language>zh-cn</language>
<copyright>老丁</copyright>
<pubDate>Wed, 22 Oct 2008 09:25:56 GMT</pubDate>
<dc:date>2008-10-22T09:25:56Z</dc:date>
<dc:language>zh-cn</dc:language>
<dc:rights>老丁</dc:rights>
<item>
<title>title1</title>
<link>http://www.blogjava.net/laoding</link>
<description>laoding's javablog</description>
<enclosure url="111" />
<category>1111</category>
<guid>http://www.blogjava.net/laoding</guid>
<dc:creator>laoding</dc:creator>
</item>
<item>
<title>title2</title>
<link>http://www.blogjava.net/laoding</link>
<description>laoding's javablog</description>
<enclosure url="222" />
<category>2222</category>
<guid>http://www.blogjava.net/laoding</guid>
<dc:creator>laoding</dc:creator>
</item>
<item>
<title>333</title>
<link>33333</link>
<description>3333</description>
<enclosure url="333" />
<category>333</category>
<guid>33333</guid>
<dc:creator>33</dc:creator>
</item>
<item>
<title>444</title>
<link>44444</link>
<description>44444</description>
<enclosure url="4444" />
<category>4444</category>
<guid>44444</guid>
<dc:creator>4444</dc:creator>
</item>
</channel>
</rss>
这个xml文件就是符合rss格式的,发布到web工程中就可以被RSS阅读器订阅了,嘿嘿
具体的应用就要靠自己去扩展了,有疑问请留言。
posted on 2008-10-22 17:29
老丁 阅读(1023)
评论(3) 编辑 收藏 所属分类:
RSS聚合