1.通过Rome插件可以简化对这两种规范文件的解析难度!Rss,Atom一般用在网站订阅功能,可以实现网络资源共享,用户可以通过客户端软件直接浏览各大网站已订阅的文章。简单的来说,电子订阅的特点就是,可以将多个网站上频繁更新的内容如:博客,新闻,聚合到一个网页列表上。ff可以下载sage插件解析,IE7可以直接解析。以下是读写操作的代码示例:
其中FeedType为以下同种中的一种
用ROME ,就可以很简单的生成rss, atom或者将rss转化成atom, 等操作..
这个插件需要3个jar包 jdom.jar rome-0.9.jar rome-fetcher-0.9.jar
2.通过rssutils插件可以对RssXml进行读操作,引入jar包之后直接可以通过在jsp页写标签进行输出,优点:使用简单。缺点:仅仅只能对RssXml文件进行读取操作。
需要rssutils.jar
package rss;
import java.io.FileWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
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.SyndFeedOutput;
public class RssWrite {
public static void main(String[] args) throws Exception {
DateFormat dateParser = new SimpleDateFormat("yyyyMMddhhmmss");
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("atom_1.0");
feed.setTitle("Sample Feed (created with Rome)");
feed.setLink("http://rome.dev.java.net");
feed.setDescription("This feed has been created using Rome (Java syndication utilities");
feed.setEncoding("utf-8");//默认是utf-8
List entries = new ArrayList();
SyndEntry entry;
SyndContent description;
entry = new SyndEntryImpl(); // feed其中的一个 entry, 即其中一篇文章
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("hello caohaigang");
entry.setDescription(description);
entries.add(entry);
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("<img src=\"http://farm4.static.flickr.com/3026/2645744338_bf6a7e9c57_m.jpg\" />");
entry.setDescription(description);
entries.add(entry);
feed.setEntries(entries); // 设置feed的文章列表
String path = "c:/xml";
File f = new File(path);
if(!f.exists()){
f.mkdirs();
}
String fileName = dateParser.format(new Date())+".xml";
//FileWriter输出的XML文件为gb2312编码,这在linux下无法使用
//Writer writer = new FileWriter(path + "/" + fileName);
//要和正文编码设置一致
Writer writer = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8");
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed,writer); // 向客户端输出xml
writer.close();
System.out.println("The feed has been written to the file ["+fileName+"]");
}
}
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.fetcher.FeedFetcher;
import com.sun.syndication.fetcher.impl.FeedFetcherCache;
import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache;
import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher;
public class RssReader {
public static void main(String[] args) throws Exception {
FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance();
FeedFetcher feedFetcher = new HttpURLFeedFetcher(feedInfoCache);
URL url = new URL("http://www.china-pub.com/rss/FeedPub.asp?id=3");
//url是XML文件, 如http://feed.feedsky.com/xxx.xml
SyndFeed feed = feedFetcher.retrieveFeed(url);
List entryList = feed.getEntries();
for (int i = 0; i < entryList.size(); i++) {
SyndEntry entry = (SyndEntry) entryList.get(i);
System.out.println("Published Date: " + entry.getPublishedDate());
System.out.println("Title: " + entry.getTitle());
System.out.println("Link: " + entry.getLink());
SyndContent sc = entry.getDescription();
if(sc!=null){
System.out.println("Description: " + sc.getValue());
}
System.out.println("------------------------------");
}
}
}
其中FeedType为以下同种中的一种
public static final String RSS_090 = "rss_0.9";
public static final String RSS_092 = "rss_0.92";
public static final String RSS_093 = "rss_0.93";
public static final String RSS_094 = "rss_0.94";
public static final String RSS_091_Netscape = "rss_0.91";
public static final String RSS_091_Userland = "rss_0.91";
public static final String RSS_100 = "rss_1.0";
public static final String RSS_200 = "rss_2.0";
public static final String ATOM_030 = "atom_0.3";
public static final String ATOM_100 = "atom_1.0";
用ROME ,就可以很简单的生成rss, atom或者将rss转化成atom, 等操作..
这个插件需要3个jar包 jdom.jar rome-0.9.jar rome-fetcher-0.9.jar
2.通过rssutils插件可以对RssXml进行读操作,引入jar包之后直接可以通过在jsp页写标签进行输出,优点:使用简单。缺点:仅仅只能对RssXml文件进行读取操作。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/WEB-INF/rssutils.tld" prefix="rss" %>
<rss:feed
url="http://dev.csdn.net/rss/2.xml"
feedId="example1"/>
<b>Image: </b><rss:channelImage feedId="example1" asLink="true"/><br>
<b>Title: </b><rss:channelTitle feedId="example1"/><br>
<b>Link: </b><rss:channelLink feedId="example1" asLink="true"/><br>
<b>Description: </b><rss:channelDescription feedId="example1"/><br>
<ul>
<rss:forEachItem feedId="example1">
<li><rss:itemTitle feedId="example1"/><br>
<rss:itemLink feedId="example1"/><br>
<rss:itemDescription feedId="example1"/><br>
</rss:forEachItem>
</ul>
需要rssutils.jar