使用ROME解析RSS/ATOM

ROME is an set of open source Java tools for parsing, generating and publishing RSS and Atom feeds. The core ROME library depends only on the JDOM XML parser and supports parsing, generating and converting all of the popular RSS and Atom formats including RSS 0.90, RSS 0.91 Netscape, RSS 0.91 Userland, RSS 0.92, RSS 0.93, RSS 0.94, RSS 1.0, RSS 2.0, Atom 0.3, and Atom 1.0. You can parse to an RSS object model, an Atom object model or an abstract SyndFeed model that can model either family of formats.

下面是一个使用ROME解析google博客输出RSS的java代码
import java.io.IOException;
import java.net.MalformedURLException;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.io.FeedException;

/**
 *
 * 使用rome rss和atom feeds工具解析google的博客rss
 *
 */
public class TestRome {

    public static void main(String[] args) {

            // 如果程序位于防火墙后面,就需要在程序中加上一些Proxy设置。
            java.util.Properties systemSettings = System.getProperties();
            systemSettings.put("http.proxyHost", "wsay.net");
            systemSettings.put("http.proxyPort", "8080");
            System.setProperties(systemSettings);


            String urlStr = "http://blogsearch.google.cn/blogsearch_feeds?hl=zh-CN&um=1&q=site:itindex.net&lr=&ie=utf-8&num=10&output=rss";
//            java.net.URLConnection feedUrl = new java.net.URL("file:D:/atom.xhtml").openConnection();
            java.net.URLConnection feedUrl=null;
            try {
                feedUrl = new java.net.URL(urlStr).openConnection();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            feedUrl.setRequestProperty("User-Agent",
                    "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; http://itindex.net)");
            com.sun.syndication.io.SyndFeedInput input = new com.sun.syndication.io.SyndFeedInput();
            com.sun.syndication.feed.synd.SyndFeed feed=null;
            try {
                feed = input
                        .build(new com.sun.syndication.io.XmlReader(feedUrl));
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (FeedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String feedType = feed.getFeedType();// 获取原feed属于哪种格式

            System.out.println(feedType);
           
            java.util.List list = feed.getEntries();
            for (int i = 0; i < list.size(); i++) {
                com.sun.syndication.feed.synd.SyndEntry entry = (com.sun.syndication.feed.synd.SyndEntry) list
                        .get(i);

                String entryTitle = entry.getLink();
                System.out.println("PublishedDate:"+entry.getPublishedDate());
                System.out.println("Title:"+entry.getTitle());
                System.out.println("Link:"+entry.getLink());
                System.out.println("category:"+entry.getCategories());
                System.out.println("Description:"+entry.getDescription());
                if(entry.getContents().size()>0){
                    SyndContent syndContent=(SyndContent)entry.getContents().get(0);
                    System.out.println("Content:"+syndContent.getValue());
                }

                System.out.println("----------------------------:");
            }

    }

}

你可能感兴趣的:(java,xml,other)