最后努力一把,争取今天上排名,提前一天完成任务。好高兴。
这个同样是给初学的一点东西,请大家不要见笑。
在这里我用 dom4j来写xml,希望ice不要再来找我麻烦了,我这个人喜欢用File啊,呵呵,虽然比较麻烦。
首先我们去下载一个dom4j,然后在导入到 Eclipse 的 lib里。
这里下载
目标 在《周博通》里,可以更新等。
完成后截图
代码非常简单。不用解说了吧,呵呵
package org.dong.rss;
import java.io.File;
import java.io.FileWriter;
import java.util.Date;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
public class RssGen {
public static int createRssFile(String filename) {
/** 返回操作结果, 0表失败, 1表成功 */
int returnValue = 0;
/** 建立document对象 */
Document document = DocumentHelper.createDocument();
/** 建立channel */
Element channelElement = document.addElement("channel");
/** 加入一行注释 */
try {
channelElement.addComment("myao's rss channel demo");
// channel
Element titleElement = channelElement.addElement("title");
Element desElement = channelElement.addElement("description");
Element linkElement = channelElement.addElement("link");
// language
Element langElement = channelElement.addElement("language");
Element pubDateElement = langElement.addElement("pubDate");
Element lastBuildDateElement = langElement
.addElement("lastBuildDate");
Element generatorElement = langElement.addElement("generator");
// item
/** 为channel设置内容 */
titleElement.setText("myao's rss title");
desElement.setText("myao's channel");
linkElement.setText("http://www.blogjava.net/myao");
// 为language设置内容
langElement.setText("zh-cn");
Date dt = new Date();
pubDateElement.setText(dt.toString());
lastBuildDateElement.setText(dt.toString());
generatorElement.setText("myao");
for (int fori = 0; fori < 10; fori++) {
Element itemElement = channelElement.addElement("item");
Element itemtitle = itemElement.addElement("title");
Element itemdes = itemElement.addElement("description");
Element itemlink = itemElement.addElement("link");
Element itemcreator = itemElement.addElement("creator");
Element itemauthor = itemElement.addElement("author");
Element itempubDate = itemElement.addElement("pubDate");
//为单个文章设置内容
itemauthor.setText("myao");
itemcreator.setText("myao");
StringBuffer title = new StringBuffer();
title.append("article ");
title.append(fori);
title.append(new Date());
itempubDate.setText(dt.toString());
itemtitle.setText(title.toString());
itemlink.setText("http://www.blogjava.net/myao");
itemdes.setText("article des" + title.toString());
}
} catch (Exception e) {
System.out.println(e);
}
try {
/** 将document中的内容写入文件中 */
XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)));
writer.write(document);
writer.close();
/** 执行成功,需返回1 */
returnValue = 1;
} catch (Exception ex) {
ex.printStackTrace();
}
return returnValue;
}
//测试程序
public static void main(String[] args) {
if (createRssFile("c:/rssfile.xml") == 1) {
System.out.println("gen suc");
} else {
System.out.println("gen fail");
}
}
}
这个同样是给初学的一点东西,请大家不要见笑。
在这里我用 dom4j来写xml,希望ice不要再来找我麻烦了,我这个人喜欢用File啊,呵呵,虽然比较麻烦。
首先我们去下载一个dom4j,然后在导入到 Eclipse 的 lib里。
这里下载
目标 在《周博通》里,可以更新等。
完成后截图
代码非常简单。不用解说了吧,呵呵
package org.dong.rss;
import java.io.File;
import java.io.FileWriter;
import java.util.Date;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
public class RssGen {
public static int createRssFile(String filename) {
/** 返回操作结果, 0表失败, 1表成功 */
int returnValue = 0;
/** 建立document对象 */
Document document = DocumentHelper.createDocument();
/** 建立channel */
Element channelElement = document.addElement("channel");
/** 加入一行注释 */
try {
channelElement.addComment("myao's rss channel demo");
// channel
Element titleElement = channelElement.addElement("title");
Element desElement = channelElement.addElement("description");
Element linkElement = channelElement.addElement("link");
// language
Element langElement = channelElement.addElement("language");
Element pubDateElement = langElement.addElement("pubDate");
Element lastBuildDateElement = langElement
.addElement("lastBuildDate");
Element generatorElement = langElement.addElement("generator");
// item
/** 为channel设置内容 */
titleElement.setText("myao's rss title");
desElement.setText("myao's channel");
linkElement.setText("http://www.blogjava.net/myao");
// 为language设置内容
langElement.setText("zh-cn");
Date dt = new Date();
pubDateElement.setText(dt.toString());
lastBuildDateElement.setText(dt.toString());
generatorElement.setText("myao");
for (int fori = 0; fori < 10; fori++) {
Element itemElement = channelElement.addElement("item");
Element itemtitle = itemElement.addElement("title");
Element itemdes = itemElement.addElement("description");
Element itemlink = itemElement.addElement("link");
Element itemcreator = itemElement.addElement("creator");
Element itemauthor = itemElement.addElement("author");
Element itempubDate = itemElement.addElement("pubDate");
//为单个文章设置内容
itemauthor.setText("myao");
itemcreator.setText("myao");
StringBuffer title = new StringBuffer();
title.append("article ");
title.append(fori);
title.append(new Date());
itempubDate.setText(dt.toString());
itemtitle.setText(title.toString());
itemlink.setText("http://www.blogjava.net/myao");
itemdes.setText("article des" + title.toString());
}
} catch (Exception e) {
System.out.println(e);
}
try {
/** 将document中的内容写入文件中 */
XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)));
writer.write(document);
writer.close();
/** 执行成功,需返回1 */
returnValue = 1;
} catch (Exception ex) {
ex.printStackTrace();
}
return returnValue;
}
//测试程序
public static void main(String[] args) {
if (createRssFile("c:/rssfile.xml") == 1) {
System.out.println("gen suc");
} else {
System.out.println("gen fail");
}
}
}