`
gaozzsoft
  • 浏览: 413887 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

Rss,Atom聚合规范的XML文件解析(Rome,rssutils)

    博客分类:
  • XML
阅读更多
1.通过Rome插件可以简化对这两种规范文件的解析难度!Rss,Atom一般用在网站订阅功能,可以实现网络资源共享,用户可以通过客户端软件直接浏览各大网站已订阅的文章。简单的来说,电子订阅的特点就是,可以将多个网站上频繁更新的内容如:博客,新闻,聚合到一个网页列表上。ff可以下载sage插件解析,IE7可以直接解析。以下是读写操作的代码示例:

Java代码
1.package rss;  
2. 
3.import java.io.FileWriter;  
4.import java.io.Writer;  
5.import java.text.DateFormat;  
6.import java.text.SimpleDateFormat;  
7.import java.util.ArrayList;  
8.import java.util.Date;  
9.import java.util.List;  
10. 
11.import com.sun.syndication.feed.synd.SyndContent;  
12.import com.sun.syndication.feed.synd.SyndContentImpl;  
13.import com.sun.syndication.feed.synd.SyndEntry;  
14.import com.sun.syndication.feed.synd.SyndEntryImpl;  
15.import com.sun.syndication.feed.synd.SyndFeed;  
16.import com.sun.syndication.feed.synd.SyndFeedImpl;  
17.import com.sun.syndication.io.SyndFeedOutput;  
18. 
19.public class RssWrite {  
20. 
21.    public static void main(String[] args) throws Exception {  
22.        DateFormat dateParser = new SimpleDateFormat("yyyyMMddhhmmss");  
23. 
24.        SyndFeed feed = new SyndFeedImpl();  
25.        feed.setFeedType("atom_1.0");  
26.        feed.setTitle("Sample Feed (created with Rome)");  
27.    feed.setLink("http://rome.dev.java.net");  
28.    feed.setDescription("This feed has been created using Rome (Java syndication utilities");  
29.    feed.setEncoding("utf-8");//默认是utf-8  
30. 
31.    List entries = new ArrayList();  
32.    SyndEntry entry;  
33.    SyndContent description;  
34. 
35.    entry = new SyndEntryImpl(); // feed其中的一个 entry, 即其中一篇文章  
36. 
37.    entry.setTitle("Rome v1.0");  // 设置文章标题  
38.    entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome01");  
39.    entry.setPublishedDate(new Date());  
40.    description = new SyndContentImpl(); // 文章的描述  
41.    description.setType("text/plain");  
42.    description.setValue("hello caohaigang");  
43.    entry.setDescription(description);  
44.    entries.add(entry);  
45. 
46.    entry = new SyndEntryImpl();  
47.    entry.setTitle("Rome v1.0");  
48.    entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome01");  
49.    entry.setPublishedDate(new Date());  
50.    description = new SyndContentImpl();  
51.    description.setType("text/plain");  
52.    description.setValue("<img src=\"http://farm4.static.flickr.com/3026/2645744338_bf6a7e9c57_m.jpg\" />");  
53.    entry.setDescription(description);  
54.    entries.add(entry);  
55. 
56.    feed.setEntries(entries); // 设置feed的文章列表  
57.    String path = "c:/xml";  
58.    File f = new File(path);         
59.        if(!f.exists()){  
60.            f.mkdirs();    
61.        }  
62.    String fileName = dateParser.format(new Date())+".xml";  
63.    //FileWriter输出的XML文件为gb2312编码,这在linux下无法使用  
64.    //Writer writer = new FileWriter(path + "/" + fileName);  
65.    //要和正文编码设置一致  
66.    Writer writer = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8");  
67.    SyndFeedOutput output = new SyndFeedOutput();  
68.    output.output(feed,writer); // 向客户端输出xml  
69.    writer.close();  
70. 
71.    System.out.println("The feed has been written to the file ["+fileName+"]");  
72.    }  
73.} 
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+"]");
}
}

Java代码
1.import com.sun.syndication.feed.synd.SyndEntry;  
2.import com.sun.syndication.feed.synd.SyndFeed;  
3.import com.sun.syndication.fetcher.FeedFetcher;  
4.import com.sun.syndication.fetcher.impl.FeedFetcherCache;  
5.import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache;  
6.import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher;  
7. 
8.public class RssReader {  
9. 
10.    public static void main(String[] args) throws Exception {  
11.        FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance();  
12.        FeedFetcher feedFetcher = new HttpURLFeedFetcher(feedInfoCache);  
13. 
14.        URL url = new URL("http://www.china-pub.com/rss/FeedPub.asp?id=3");  
15.        //url是XML文件, 如http://feed.feedsky.com/xxx.xml  
16.        SyndFeed feed = feedFetcher.retrieveFeed(url);  
17.        List entryList = feed.getEntries();  
18.        for (int i = 0; i < entryList.size(); i++) {  
19.            SyndEntry entry = (SyndEntry) entryList.get(i);  
20.            System.out.println("Published Date: " + entry.getPublishedDate());  
21.            System.out.println("Title: " + entry.getTitle());  
22.            System.out.println("Link: " + entry.getLink());  
23.            SyndContent sc = entry.getDescription();  
24.            if(sc!=null){  
25.                System.out.println("Description: " + sc.getValue());  
26.            }  
27.            System.out.println("------------------------------");  
28.        }  
29.    }  
30.} 
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为以下同种中的一种

Java代码
1.public static final String RSS_090 = "rss_0.9";  
2.public static final String RSS_092 = "rss_0.92";  
3.public static final String RSS_093 = "rss_0.93";  
4.public static final String RSS_094 = "rss_0.94";  
5.public static final String RSS_091_Netscape = "rss_0.91";  
6.public static final String RSS_091_Userland = "rss_0.91";  
7.public static final String RSS_100 = "rss_1.0";  
8.public static final String RSS_200 = "rss_2.0";  
9.public static final String ATOM_030 = "atom_0.3";  
10.public static final String ATOM_100 = "atom_1.0"; 
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文件进行读取操作。

Xml代码
1.<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 
2.<%@ taglib uri="/WEB-INF/rssutils.tld" prefix="rss" %> 
3. 
4.<rss:feed 
5.url="http://dev.csdn.net/rss/2.xml" 
6.feedId="example1"/> 
7.<b>Image: </b><rss:channelImage feedId="example1" asLink="true"/><br> 
8.<b>Title: </b><rss:channelTitle feedId="example1"/><br> 
9.<b>Link: </b><rss:channelLink feedId="example1" asLink="true"/><br> 
10.<b>Description: </b><rss:channelDescription feedId="example1"/><br> 
11.<ul> 
12.  <rss:forEachItem feedId="example1"> 
13.    <li><rss:itemTitle feedId="example1"/><br> 
14.        <rss:itemLink feedId="example1"/><br> 
15.        <rss:itemDescription feedId="example1"/><br> 
16.  </rss:forEachItem> 
17.</ul> 
<%@ 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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics