Last year, I made a project to support RSS output. I was depressed that I could not find a method to support RSS output. Later I thought about it carefully. RSS output is actually generating an XML file on the server, write the information to the server later. This involves reading and writing XML files. Later I chose JDOM. The Code is as follows. If you cannot understand it, contact me: 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) // Add information to the RSS File
{
Date = new date ();
This. DTO = DTO;
String albumname = DTO. getalbumname ();
String artistName = dto. getArtistName (); // artist name
DateFormat format = DateFormat. getDateTimeInstance ();
String time = format. format (date); // date
Int albumId = dto. getAlbumId (). intValue (); // album id
String id = String. valueOf (albumId );
String url = "http: // localhost: 8080/musicWeb/getSongAction. do? CategoryId = "+ id;
System. out. println ("the current time is:" + time );
String atitle = artistName + "new album --" + albumName + "-- has arrived at this site. Please refer ";
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 );
}
}
}
A sample of an Rss file is attached:
<? Xml version = "1.0" encoding = "UTF-8"?>
& Lt; rss version = "2.0" & gt;
<Channel>
<Title> CSDN Technical Documentation Center </title>
<Description> CSDN Technical Documentation Center </description>
<Link> http://dev.csdn.net/</link>
<Language> zh-cn </language>
<Docs> CSDN. NET Develop Article </docs>
<Generator> Rss Generator By CSDN. NET </generator>
<Item>
<Title> DIY creative website error information </title>
<Link> http://dev.csdn.net/article/74387.shtm </link>
<Author> nbpig </author>
<PubDate> Fri, 05 Aug 2005 00:17:00 GMT </pubDate>
</Item>
<Item>
<Title> A Java menu encapsulated by JAVA to change LookAndFeel can be directly dragged (original) in visual editing. </title>
<Link> http://dev.csdn.net/article/74380.shtm </link>
<Author> badqiu </author>
<PubDate> Thu, 04 Aug 2005 20:18:00 GMT </pubDate>
</Item>
<Item>
<Title> for non-java.util.zip files, use ant. jar to create zip files and use relevant Chinese solutions </title>
<Link> http://dev.csdn.net/article/74374.shtm </link>
<Author> thefirstwind </author>
<PubDate> Thu, 04 Aug 2005 17:40:00 GMT </pubDate>
</Item>
<Item>
<Title> Apache ant Preliminary Experience (Note) </title>
<Link> http://dev.csdn.net/article/74371.shtm </link>
<Author> fly _ dragon </author>
<Pubdate> Thu, 04 Aug 2005 16:07:00 GMT </pubdate>
</Item>
<Item>
<Title> Questions for Java programmers </title>
<Link> http://dev.csdn.net/article/74364.shtm </link>
<Author> gqy789 </author>
<Pubdate> Thu, 04 Aug 2005 15:18:00 GMT </pubdate>
</Item>
<Item>
<Title> Use ant in eclipse Quick Start Guide </title>
<Link> http://dev.csdn.net/article/74362.shtm </link>
<Author> gqy789 </author>
<Pubdate> Thu, 04 Aug 2005 15:10:00 GMT </pubdate>
</Item>
</Channel>
</RSS>