去年做了个项目,要求支持RSS输出,郁闷老找不到支持RSS输出的方法,后来仔细一想,RSS输出其实也就是在服务器生成一个XML文件,以后把信息写到服务器就行了。这又涉及到读写XML文件了,后来我选择了JDOM。代码如下,看不明白的可以跟我联系:danke QQ:56355337
package music;
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import java.io.*;
import java.util.*;
import java.text.DateFormat;
public class RSSCreater
{
AlbumDto dto=null;
public RSSCreater(AlbumDto dto,String dir)
{
Date date=new Date();
this.dto=dto;
String albumName=dto.getAlbumName();
String artistName=dto.getArtistName();
DateFormat format=DateFormat.getDateTimeInstance();
String time=format.format(date);
int albumId=dto.getAlbumId().intValue();
String id=String.valueOf(albumId);
String url="http://localhost:8080/musicWeb/getSongAction.do?categoryId="+id;
System.out.println("现在时间是:"+time);
String atitle=artistName+"的新专辑-- "+albumName+"--已经到达本站,欢迎查看";
try
{
String truePath=dir+"/"+"index.xml";
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(new FileInputStream(truePath));
Element root = doc.getRootElement();
java.util.List books = root.getChildren("channel");
Element item=(Element)books.get(0);
Element it=new Element("item");
Element title=new Element("title").setText(atitle);
Element lint=new Element("link").setText(url);
Element datetime=new Element("pubDate").setText(time);
Element guid=new Element("guid").setText(url);
it.addContent(title);
it.addContent(lint);
it.addContent(guid);
it.addContent(datetime);
item.addContent(it);
File files=new File(truePath);
Writer xmlwriter=new FileWriter(files);
FileOutputStream outstream=new FileOutputStream(files);
XMLOutputter fmt=new XMLOutputter();
fmt.output(doc,xmlwriter);
fmt.output(doc,outstream);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
本文地址:http://com.8s8s.com/it/it9990.htm