<?xml version="1.0"?>
<!-- RSS generated by Radio UserLand v8.0.5 on 9/30/2002; 4:00:00 AM Pacific -->
<rss version="2.0" xmlns:blogChannel="http://backend.userland.com/blogChannelModule">
<channel>
<title>Scripting News</title>
<link>http://www.scripting.com/</link>
<description>A weblog about scripting and stuff like that.</description>
<language>en-us</language>
<blogChannel:blogRoll>http://radio.weblogs.com/0001015/userland/scriptingNewsLeftLinks.opml</blogChannel:blogRoll>
<blogChannel:mySubscriptions>http://radio.weblogs.com/0001015/gems/mySubscriptions.opml</blogChannel:mySubscriptions>
<blogChannel:blink>http://diveinto.org/</blogChannel:blink>
<copyright>Copyright 1997-2002 Dave Winer</copyright>
<lastBuildDate>Mon, 30 Sep 2002 11:00:00 GMT</lastBuildDate>
<docs>http://backend.userland.com/rss</docs>
<generator>Radio UserLand v8.0.5</generator>
<category domain="Syndic8">1765</category>
<managingEditor>[email protected]</managingEditor>
<webMaster>[email protected]</webMaster>
<ttl>40</ttl>
<item>
<description>
your article details
</description>
<pubDate>Mon, 30 Sep 2002 01:56:02 GMT</pubDate>
<guid>http://scriptingnews.userland.com/backissues/2002/09/29#When:6:56:02PM</guid>
</item>
</channel>
</rss>
<!-- RSS generated by Radio UserLand v8.0.5 on 9/30/2002; 4:00:00 AM Pacific -->
<rss version="2.0" xmlns:blogChannel="http://backend.userland.com/blogChannelModule">
<channel>
<title>Scripting News</title>
<link>http://www.scripting.com/</link>
<description>A weblog about scripting and stuff like that.</description>
<language>en-us</language>
<blogChannel:blogRoll>http://radio.weblogs.com/0001015/userland/scriptingNewsLeftLinks.opml</blogChannel:blogRoll>
<blogChannel:mySubscriptions>http://radio.weblogs.com/0001015/gems/mySubscriptions.opml</blogChannel:mySubscriptions>
<blogChannel:blink>http://diveinto.org/</blogChannel:blink>
<copyright>Copyright 1997-2002 Dave Winer</copyright>
<lastBuildDate>Mon, 30 Sep 2002 11:00:00 GMT</lastBuildDate>
<docs>http://backend.userland.com/rss</docs>
<generator>Radio UserLand v8.0.5</generator>
<category domain="Syndic8">1765</category>
<managingEditor>[email protected]</managingEditor>
<webMaster>[email protected]</webMaster>
<ttl>40</ttl>
<item>
<description>
your article details
</description>
<pubDate>Mon, 30 Sep 2002 01:56:02 GMT</pubDate>
<guid>http://scriptingnews.userland.com/backissues/2002/09/29#When:6:56:02PM</guid>
</item>
</channel>
</rss>
http://download.csdn.net/source/493025这个或许对你有用。
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedOutput;public class Test {
public static void main(String [] args){
// 构造一个SyndFeed新对象
SyndFeed feed = new SyndFeedImpl();
// 设置Feed类型
feed.setFeedType("rss_2.0");
// 设置Feed基本信息
feed.setTitle("测试Feed");
feed.setLink("http://www.thedevlog.com");
feed.setDescription("测试使用ROME来构造Feed数据");
feed.setLanguage("en-us");
feed.setCopyright("Copyright 1997-2002 Dave Winer");
feed.setPublishedDate(new Date());
// 构造Feed items列表
List<SyndEntry> entries = new ArrayList<SyndEntry>();
SyndEntry entry;
SyndContent description;
// 构造一个新的节点并添加到列表中
entry = new SyndEntryImpl();
entry.setTitle("ROME v1.0");
entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome01");
entry.setPublishedDate(new Date());
description = new SyndContentImpl();
description.setType("text/plain");
description.setValue("Initial release of ROME");
entry.setDescription(description);
entries.add(entry);
// 设置更多的节点...
// 将节点列表赋给feed对象
feed.setEntries(entries);
// 输出feed
try {
FileWriter writer = new FileWriter("./test.xml");
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed, writer);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FeedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这样可以实现一部分,但是命名空间,还有某些属性无法得到。不能做到与上例一样,求解.
import java.util.Date;
import java.util.List;import com.sun.syndication.feed.rss.Channel;
import com.sun.syndication.feed.rss.Description;
import com.sun.syndication.feed.rss.Guid;
import com.sun.syndication.feed.rss.Item;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.WireFeedOutput;public class CreateParse {
public static void main(String[] args) {
/*
* 根据Channel源码提供的英文,Channel对象有两个构造器,一个默认的无参构造器用于clone对象,一个是有参的
* 我们自己指定的必须使用有参数的
* (因为我们需要许可证),指构造方法必须要创建一个type(版本),这个type不能随便写,必须要以rss_开头的版本号 Licensed
* under the Apache License, Version 2.0 (the "License");
* 因为当前版本是2.0,所以就是rss_2.0,必须是rss_2.0否则会抛异常,该源码中写的已经很明白。
*/
Channel channel = new Channel("rss_2.0");
channel.setTitle("channel标题");// 网站标题
channel.setDescription("channel的描述");// 网站描述
channel.setLink("www.shlll.net");// 网站主页链接
channel.setEncoding("utf-8");// RSS文件编码
channel.setLanguage("zh-cn");// RSS使用的语言
channel.setLastBuildDate(new Date());
channel.setDocs("http://backend.userland.com/rss");
channel.setGenerator("Radio UserLand v8.0.5");
channel.setManagingEditor("[email protected]");
channel.setWebMaster("[email protected]");
channel.setTtl(5);// time to live的简写,在刷新前当前RSS在缓存中可以保存多长时间(分钟)
channel.setCopyright("版权声明");// 版权声明
channel.setPubDate(new Date());// RSS发布时间
List<Item> items = new ArrayList<Item>();// 这个list对应rss中的item列表
Item item = new Item();// 新建Item对象,对应rss中的<item></item>
item.setAuthor("hxliu");// 对应<item>中的<author></author>
item.setTitle("新闻标题");// 对应<item>中的<title></title>
item.setGuid(new Guid());// GUID=Globally Unique Identifier
// 为当前新闻指定一个全球唯一标示,这个不是必须的
item.setPubDate(new Date());// 这个<item>对应的发布时间
item.setComments("注释");// 代表<item>节点中的<comments></comments>
// 新建一个Description,它是Item的描述部分
Description description = new Description();
description.setValue("新闻主题");// <description>中的内容
item.setDescription(description);// 添加到item节点中
items.add(item);// 代表一个段落<item></item>,
channel.setItems(items);
// 用WireFeedOutput对象输出rss文本
WireFeedOutput out = new WireFeedOutput();
try {
System.out.println(out.outputString(channel));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (FeedException e) {
e.printStackTrace();
}
}
}这样可以得到与示例相同的节点,但是没有名称空间。求解。