高山流水
msgbartop
动之则分,静之则合,无过不及,随曲就伸。
msgbarbottom

25 九 06 一个生成RSS的JavaBean


//新闻rss

//jnuzyx.2006.09.20

//需要JDOM1.0

//需要数据库链接类

package rss;

import java.io.FileOutputStream;

import java.util.Date;

import java.text.SimpleDateFormat;

import org.jdom.Attribute;

import org.jdom.Document;

import org.jdom.Element;

import org.jdom.output.XMLOutputter;

import org.jdom.output.Format;

import org.jdom.CDATA;

import java.sql.*;

import *.*Pool;

public class news

{

//

public
static void rssnews(String dir) throws Exception {

Element root = new Element(”rss”);

root.setAttribute(new Attribute(”version”, “2.0″));

Document doc = new
Document(root);
//将根元素植入

Element channel = new Element(”channel”);

root.addContent(channel);

Element Ntitle = new Element(”title”);

Ntitle.addContent(”暨南大学图书馆新闻订阅”);

channel.addContent(Ntitle);

Element Nlink = new Element(”link”);

Nlink.addContent(”http://lib.jnu.edu.cn/“);

channel.addContent(Nlink);

Element Ndescription = new Element(”description”);

Ndescription.addContent(”暨南大学图书馆新闻订阅”);

channel.addContent(Ndescription);

Element Nlanguage = new Element(”language”);

Nlanguage.addContent(”zh-cn”);

channel.addContent(Nlanguage);

Element Ncopyright = new Element(”copyright”);

Ncopyright.addContent(”Copyright 2006 暨南大学图书馆. All Rights
Reserved.”);

channel.addContent(Ncopyright);

Element NwebMaster = new Element(”webMaster”);

NwebMaster.addContent(”暨南大学图书馆”);

channel.addContent(NwebMaster);

Element NpubDate = new Element(”pubDate”);

java.util.Date now=new java.util.Date();

NpubDate.addContent(FormatRssDate(now));

channel.addContent(NpubDate);

Element NlastBuildDate = new Element(”lastBuildDate”);

NlastBuildDate.addContent(FormatRssDate(now));

channel.addContent(NlastBuildDate);

Element Ngenerator = new Element(”generator”);

Ngenerator.addContent(”JNULRSS 1.0(beta)”);

channel.addContent(Ngenerator);

//读入数据库数据

//*.*Pool pool = new ****;

ResultSet rs = pool.getRs(”$sql 语句$”);

//增加ITEM

String strContent;

while(rs.next()){

Element student = new
Element(”item”);//生成元素
student.addContent(new
Element(”title”).addContent(rs.getString(”*”)));

student.addContent(new Element(”link”).addContent(”$url$”)));

student.addContent(new
Element(”author”).addContent(”jnuzyx”));

student.addContent(new
Element(”category”).addContent(”暨南大学图书馆新闻订阅”));

student.addContent(new
Element(”pubdate”).addContent(FormatRssDate($date$)));

if(rs.getString(”$content$”)!=null)

{

strContent =
rs.getString(”$content$”).substring(0, 320)+”….”;

}

else

{

strContent = “请点击查看全文….”;

}

student.addContent(new Element(”description”).addContent(new
CDATA(strContent)));

channel.addContent(student);

}

//关闭

if(rs!=null)

{

rs.close();

}

rs = null;

//输出

try{

XMLOutputter XMLOut = new
XMLOutputter(Format.getPrettyFormat());

XMLOut.output(doc, new FileOutputStream(dir + “news.xml”));

}catch (java.io.IOException e) {

e.printStackTrace();

}

}

public
static String FormatRssDate(Date dt) {

SimpleDateFormat RssFmtDt=new SimpleDateFormat(”EEE, dd MM yyyy
HH:mm:ss z”);

return RssFmtDt.format(dt).toString();

}

}

Tags: , , ,