利用rome写rss feed生成程序

前几天转一个rome的介绍过来,现在拿它写个rss feed生成程序。rome的安装包中,有三个例程,分别是解析已有的rss feed,转换rss feed的格式以及内容聚合方面的,这个程序可以做补充。   程序思路:在更新不很频繁的发布网站上,可以生成一个通过http可访问的静态rss feed文件。为了便于调用,做成一个bean,接一个带有绝对路径的文件名作参数。


mport java.io.IOException;
import java.net.UnknownHostException;
import java.net.URL;
import java.util.List;
import java.util.Date;
import java.util.ArrayList;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.FileWriter;

import com.sun.syndication.feed.synd.SyndFeedI;
import com.sun.syndication.feed.synd.SyndContentI;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndCategory;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.io.SyndFeedOutput;
import com.sun.syndication.io.SyndFeedInput;


public class RssCreator
{
public RssCreator()
throws TRIPException, UnknownHostException, IOException
{
}

public boolean genRss(String filename)
{

try
{
。。。connect to database and locat the latest 15 records....
String outputType = "rss_1.0";
String outputFile = filename;
Writer outWriter = null;
try{
outWriter=new BufferedWriter(new FileWriter(outputFile));
}catch (Exception e){
//deal exception
}
SyndFeedI feed = new SyndFeed();
feed.setFeedType(outputType);

feed.setTitle(converttoISO(" 你的站点的名称 "));
feed.setDescription(converttoISO("描述一下站点吧。"));
feed.setAuthor("author name");
feed.setLink("http://www.yoursite.com");

List entries = new ArrayList();
feed.setEntries(entries);

for (int i=0;i<15;i++) {
SyndEntry entry = new SyndEntry();
record = search.record(hits-i);

tfield=record.field("AUTHORS");
sfld = chkValue(tfield.getValue());
entry.setAuthor( sfld );

tfield=record.field("SUBCLASS");
sfld = chkValue(tfield.getValue());
List cats=new ArrayList();
SyndCategory cat=new SyndCategory();
cat.setName(sfld);
cats.add(cat);
entry.setCategories(cats);

//entry.setContents("什么内容呢?");
tfield=record.field("ABS");
sfld = chkValue(tfield.getValue());
SyndContentI desc=new SyndContent();
desc.setValue(sfld);
entry.setDescription(desc);

tfield=record.field("URL");
sfld = chkValue(tfield.getValue());
entry.setLink(sfld);

tfield=record.field("TITLE");
sfld = chkValue(tfield.getValue());
entry.setTitle(sfld) ;
//long ldate=(long)1079511514653;
tfield=record.field("ID");//我数据库中,ID用的是系统时钟
sfld = chkValue(tfield.getValue());
Date pdate=null;
pdate=new Date(new Long(sfld).longValue());
entry.setPublishedDate(pdate);

entries.add(entry);

}

SyndFeedOutput output = new SyndFeedOutput();
output.output(feed,outWriter);
outWriter.close();


...close database....
return true;
}
catch(Exception exception)
{
close database
close file
return false
}
return true;
}
private String chkValue(String s){
if (s==null) return "";
return s;
}
public static String converttoGB(String s)
{
try
{
byte abyte0[] = s.getBytes("ISO-8859-1");
return new String(abyte0, "gb2312");
}
catch(Exception exception)
{
return s;
}
}
public static String converttoISO(String s)
{
try
{
byte abyte0[] = s.getBytes("gb2312");
return new String(abyte0, "ISO-8859-1");
}
catch(Exception exception)
{
return s;
}
}

}

  可能会遇到一些编码问题,所以可能要用到转换函数,写了几个放在代码末尾

by hedong

AddThis Social Bookmark Button

相关文档(Relevant Entries)
JNI完全手册
JBuilder9制作EXE文件
如何在Java中调用dll
JNI调用固有方法
Cache Your Data JDBC vs ADO NET
IntelliJ IDEA 5.0 新特性
用JavaHelp系统开发和交付更好的文档
《Eclipse集成开发工具》
Posted on November 29, 2006 12:01 PM | | | | TrackBacks (0)

引用地址(TRACKBACKS)
 
TrackBack URL for this entry:
http://www.wujianrong.com/mt-tb.cgi/4177

发布评论(ADD YOUR COMMENTS)
 
感谢您参与评论;发表您的意见时请保持文章的相关性;不相关的或是单纯宣传的内容可能会被删掉。您的E-mail只是用来确认您发表的文章,不会出现在网页上。
Please keep your comments relevant to this blog entry. Email addresses are never displayed, but they are required to confirm your comments.

称呼(Name):      记住我的个人信息(Remember)
邮箱(Email):
网址(URL):
评论(Add your comments):

相关内容