📄
字号:
feed.setCopyright(copyright);
}
/**
* 添加频道的子内容
* @param item <A href="mailto:{@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:{@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);
}
}
</PRE><BR><BR><BR>下面是一个示例,用于测试生成Rss:
<BR><BR>前提说明本人测试使用的Mysql数据库,数据库存在一个newsitem表用于储存Rss内容。 <BR><BR><PRE class=java name="code">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的测试", "<A href="http://www.fansgoo.com/" target=_blank>http://www.fansgoo.com/</A>", "测试生成", "zh-cn",
new Date(), "河南亿禧软件有限公司");
//设置Rss文件的生成路径
builder.buildChannel("E:\\demo.xml");
} catch (Exception e) {
e.printStackTrace();
}
}
</PRE><BR><BR>感兴趣的朋友也可以直接下载本人提供的上面类的Jar文件,方面于使用。 </DIV>
<DIV class=attachments>
<UL>
<LI><A
href="http://www.javaeye.com/topics/download/64d98954-b77e-38db-a83a-c1fcdf1fed5e">RssBuild.jar</A>
(6.4 KB)
<LI>描述:
<LI>下载次数: 28 </LI></UL></DIV>
<DIV id=topic_copyright>声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。 </DIV>
<DIV id=forum_advert_b3><SPAN>推荐链接</SPAN>
<UL class=adverts>
<LI><A href="http://www.javaeye.com/adverts/111" target=_blank><SPAN
style="FONT-WEIGHT: bold; COLOR: red">IBM
Rational开发者大会9月盛大揭幕!</SPAN></A>
<LI><A href="http://www.javaeye.com/adverts/79" target=_blank>全新 IBM SOA
数据服务开发工具</A></LI></UL></DIV></TD></TR>
<TR>
<TD class=postauthor><A
href="http://www.javaeye.com/topic/214644#">返回顶楼</A></TD>
<TD class=postcontent>
<DIV class=icons><A class=icon_www title=浏览作者的博客
href="http://jackzhangyunjie.javaeye.com/"> </A> <A
class=icon_profile title=浏览作者资料
href="http://jackzhangyunjie.javaeye.com/blog/profile"> </A> <A
class=icon_pm title=发送站内短信
href="http://jsczxy2.javaeye.com/admin/messages/new?message%5Breceiver_name%5D=jackzhangyunjie"> </A>
</DIV></TD></TR>
<TR class=sep2>
<TD colSpan=2></TD></TR></TBODY></TABLE>
<DIV class=actions>
<DIV class=buttons><A class=new_post title=发表回复
href="http://www.javaeye.com/forums/39/topics/214644/posts/new"></A></DIV>
<DIV class=wrapper></DIV></DIV>
<DIV>
<DIV class=crumbs><A href="http://www.javaeye.com/forums">论坛首页</A> <SPAN
class=arrow>→</SPAN> <A
href="http://www.javaeye.com/forums/board/Java">Java版</A> <SPAN
class=arrow>→</SPAN> <A href="http://www.javaeye.com/forums/tag/J2EE">企业应用</A>
<BR><BR>跳转论坛:<SELECT id=forum_urlname
onchange="window.location='/forums/board/'+this.value"
name=forum[urlname]><OPTION value=Java selected>Java</OPTION> <OPTION
value=Ruby>Ruby</OPTION> <OPTION value=AJAX>AJAX</OPTION> <OPTION
value=dotnet>Microsoft .Net</OPTION> <OPTION value=Tech>综合技术</OPTION> <OPTION
value=New>入门讨论</OPTION> <OPTION value=Agile>软件开发和项目管理</OPTION> <OPTION
value=Industry>行业解决方案</OPTION> <OPTION value=company>厂商论坛</OPTION> <OPTION
value=Job>招聘求职</OPTION> <OPTION value=Life>海阔天空</OPTION></SELECT>
<DIV id=forum_advert_b2><SPAN>JavaEye推荐</SPAN>
<UL class=adverts></UL></DIV></DIV>
<DIV class=quick_reply>
<FORM id=quick_reply_form onsubmit="return false;" action=/topic/214644
method=post><INPUT type=hidden value=true name=post[bbcode]>
<SPAN><STRONG>快速回复</STRONG></SPAN><SPAN><INPUT id=quote_post_id type=checkbox
value=600267 name=quote_post_id> 引用上一条消息</SPAN> <SPAN class=last>(Alt+S) <INPUT class=submit id=quick_reply_button type=submit value=提交 name=commit></SPAN>
<TEXTAREA class="required bad-words min-length-5" id=post_body name=post[body] rows=20 cols=40></TEXTAREA><BR></FORM>
<SCRIPT type=text/javascript> new HotKey("s",function() { ValidationUtils.fireSubmit("quick_reply_form"); },{altKey: true, ctrlKey: false}); new Validation("quick_reply_form", {immediate: false, onFormValidate: function(result, form){ if(result) { new Ajax.Request('/forums/39/topics/214644/posts', { onSuccess:function(response){ $('posts').insert(response.responseText); $('post_body').value = ""; $('_form_spinner_').hide(); }, parameters:Form.serialize(form) }); } }}); </SCRIPT>
</DIV></DIV>
<DIV style="DISPLAY: none">
<FORM id=post_rate_form
onsubmit="new Ajax.Request('/posts/rate', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;"
action=/posts/rate method=post><INPUT id=post_rate_id type=hidden name=id>
<INPUT id=post_rate_type type=hidden name=type> </FORM></DIV>
<SCRIPT type=text/javascript>dp.SyntaxHighlighter.ClipboardSwf = '/javascripts/syntaxhighlighter/clipboard.swf';dp.SyntaxHighlighter.HighlightAll('code', true, true); fix_image_size($$('div.postbody img'), 760);function rate_post(id, type) { Element.update('post_rate_box_' + id, '提交中...'); new Ajax.Request('/posts/rate', {asynchronous:true, evalScripts:true, parameters:{"id":id, "type":type}}); }function rate_topic(type) { Element.update('topic_rate_box', '提交中...'); new Ajax.Request('/topics/rate', {asynchronous:true, evalScripts:true, parameters:{"id":214644, "type":type}}); }</SCRIPT>
</DIV>
<DIV id=nav>
<DIV class=wrapper>
<UL>
<LI><A href="http://www.javaeye.com/">首页</A> </LI>
<LI><A href="http://www.javaeye.com/news">新闻</A> </LI>
<LI><A class=selected href="http://www.javaeye.com/forums">论坛</A> </LI>
<LI><A href="http://www.javaeye.com/ask">问答</A> </LI>
<LI><A href="http://www.javaeye.com/wiki">知识库</A> </LI>
<LI><A href="http://www.javaeye.com/blogs">博客</A> </LI>
<LI><A href="http://www.javaeye.com/groups">圈子</A> </LI>
<LI><A href="http://www.javaeye.com/jobs">招聘</A> </LI>
<LI><A href="http://www.javaeye.com/index/service">服务</A> </LI>
<LI class=last><A href="http://www.javaeye.com/search">搜索</A>
</LI></UL></DIV></DIV>
<DIV id=channel_nav>
<UL>
<LI><A href="http://java.javaeye.com/">Java</A> </LI>
<LI><A href="http://ruby.javaeye.com/">Ruby</A> </LI>
<LI><A href="http://ajax.javaeye.com/">AJAX</A> </LI>
<LI><A href="http://agile.javaeye.com/">敏捷</A> </LI>
<LI><A href="http://book.javaeye.com/">图书</A> </LI>
<LI><A href="http://oracle.javaeye.com/">Oracle</A> </LI>
<LI class=last><A href="http://primeton.javaeye.com/">普元</A>
</LI></UL></DIV></DIV>
<DIV id=footer>
<DIV id=site_nav>
<UL>
<LI><A href="http://www.javaeye.com/index/service">广告服务</A>
<LI><A href="http://webmaster.javaeye.com/">JavaEye黑板报</A>
<LI><A href="http://www.javaeye.com/index/sitemap">网站地图</A>
<LI><A href="http://www.javaeye.com/index/aboutus">关于我们</A>
<LI><A href="http://www.javaeye.com/index/contactus">联系我们</A>
<LI class=last><A href="http://www.javaeye.com/index/friend_links">友情链接</A>
</LI></UL></DIV>
<DIV id=copyright>© 2003-2008 JavaEye.com. All rights reserved. 上海炯耐计算机软件有限公司 [
沪ICP备05023328号 ] </DIV></DIV></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -