Rome使用简单说明一(基本应用)

阅读更多

Rome: http://wiki.java.net/bin/view/Javawsxml/Rome
 Rome是 java.net 上的一个开源项目,为什么叫Rome呢,按它的介绍上的说法,有个“条条大路通罗马”的意思,有些RSS的意味。Rome可能是 sun 公司从自己某个子项目中抽离出来的,package和类的命名就象j2sdk一样感觉规范。功能上支持RSS的所有版本及 Atom 0.3(Atom是和RSS类似的一种内容聚合的方式)。Rome 本身是提供API和功能实现.

package com.itfly;

import com.sun.syndication.io.impl.DateParser;

/**
 * 读取解析一个远程RSS
 * RomeReadByRemote.java
 *
 * @author mahaibo
 * @version $Revision$
 *
 * @hibernate.class lazy="true" table="RomeReadByRemote"
 */
public class RomeReadByRemote {
 
 public static void main(String[] args)
 {
  try
  {
   //如果程序位于防火墙后面,就需要在程序中加上一些Proxy设置。
   java.util.Properties systemSettings = System.getProperties();
      systemSettings.put("http.proxyHost", "wsay.net");
      systemSettings.put("http.proxyPort", "8080");
      System.setProperties(systemSettings);
      
      //有时候,可能会遇到“java.io.IOException: Server returned HTTP response code: 403 for URL”的错误信息。通常是因为服务器的安全设置不接受Java程序作为客户端访问,解决方案是设置客户端的User Agent, 示例代码如下:
      String urlStr = "http://blogsearch.google.com/blogsearch_feeds?hl=en&q=feedsky&ie=utf-8&num=10&output=rss";
      java.net.URLConnection feedUrl = new java.net.URL(urlStr).openConnection();
      feedUrl.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
      com.sun.syndication.io.SyndFeedInput input = new com.sun.syndication.io.SyndFeedInput();
      com.sun.syndication.feed.synd.SyndFeed feed = input.build(new com.sun.syndication.io.XmlReader(feedUrl));
      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(entry.getPublishedDate());
             System.out.println(entry.getTitle());
             System.out.println(entry.getLink());
             System.out.println(entry.getAuthor());
             System.out.println(entry.getAuthor());

      }
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }

}

package com.itfly;

import java.io.File;
import java.io.PrintWriter;

import com.sun.syndication.io.SyndFeedOutput;

/**
 * 读取解析一个远程RSS,并写入到一个本地文件里
 * RomeReadByRemote.java
 *
 * @author mahaibo
 * @version $Revision$
 *
 * @hibernate.class lazy="true" table="RomeReadByRemote"
 */
public class RomeReadAndWriteByRemote {
 
 private static final String feedType = "rss_1.0";
 public static void main(String[] args)
 {
  
  try
  {
   java.util.Properties systemSettings = System.getProperties();
      systemSettings.put("http.proxyHost", "mywebcache.com");
      systemSettings.put("http.proxyPort", "8080");
      System.setProperties(systemSettings);
      
      
      String urlStr = "http://blogsearch.google.com/blogsearch_feeds?hl=en&q=feedsky&ie=utf-8&num=10&output=rss";
      java.net.URLConnection feedUrl = new java.net.URL(urlStr).openConnection();
      feedUrl.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
      //读取一个远程的Rss文件
      com.sun.syndication.io.SyndFeedInput input = new com.sun.syndication.io.SyndFeedInput();
      com.sun.syndication.feed.synd.SyndFeed feed = input.build(new com.sun.syndication.io.XmlReader(feedUrl));
      
      //把读取到的结果写入到到本地一个文件里
      feed.setFeedType(feedType);//设置转换的类型后再保存到另一个文件
      SyndFeedOutput output = new SyndFeedOutput();
            output.output(feed,new File("c:/pic/1.xml"));
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }

}

package com.itfly;

import java.io.File;
import java.io.PrintWriter;

import com.sun.syndication.io.SyndFeedOutput;

/**
 * 读取解析一个本地的RSS
 * RomeReadByRemote.java
 *
 * @author mahaibo
 * @version $Revision$
 *
 * @hibernate.class lazy="true" table="RomeReadByRemote"
 */
public class RomeReadByLocal {
 private static final String feedType = "rss_2.0";
 public static void main(String[] args)
 {
  try
  {   
   //读取解析本地的一个Rss文件
      com.sun.syndication.io.SyndFeedInput input = new com.sun.syndication.io.SyndFeedInput();
      com.sun.syndication.feed.synd.SyndFeed feed = input.build(new File("c:/pic/1.xml"));
      System.out.println(feed.getFeedType());//获得原 rss是哪种格式
      //在控制台输出
      SyndFeedOutput output = new SyndFeedOutput();
      feed.setFeedType(feedType);//以哪种格式输出feed,如果这里不设置以哪种格式输出,会以它原来的格式输出
      output.output(feed, new PrintWriter(System.out));
      
      
//      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(entry.getPublishedDate());
//             System.out.println(entry.getTitle());
//             System.out.println(entry.getLink());
//             System.out.println(entry.getAuthor());
//             System.out.println(entry.getAuthor());
//
//      }
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }

}

package com.itfly;

import java.io.File;
import java.io.PrintWriter;

import com.sun.syndication.io.SyndFeedOutput;

/**
 * 读取解析一个本地的RSS,并写入另一个文件里
 * RomeReadByRemote.java
 *
 * @author mahaibo
 * @version $Revision$
 *
 * @hibernate.class lazy="true" table="RomeReadByRemote"
 */
public class RomeReadAndWriteByLocal {
 
 public static void main(String[] args)
 {
  try
  {   
   //读取解析本地的一个Rss文件
      com.sun.syndication.io.SyndFeedInput input = new com.sun.syndication.io.SyndFeedInput();
      com.sun.syndication.feed.synd.SyndFeed feed = input.build(new File("c:/pic/1.xml"));
      
      //写入到另一个文件里
      SyndFeedOutput output = new SyndFeedOutput();
      output.output(feed, new File("c:/pic/2.xml"));
      
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }

}

package com.itfly;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.SyndFeedOutput;
/**
 * 烧制(合并)作2个远程的RSS
 * RomeBurnByRemote.java
 *
 * @author mahaibo
 * @version $Revision$
 *
 * @hibernate.class lazy="true" table="RomeBurnByRemote"
 */
public class RomeBurnByRemote {

 private static final String feedType = "rss_1.0";
 public static void main(String[] args)
 {
  try
  {
   SyndFeed feed = new SyndFeedImpl();
            feed.setFeedType(feedType);

            feed.setTitle("wsay Feed");
            feed.setDescription("wsay Feed");
            feed.setAuthor("wsay");
            feed.setLink("http://www.wsay.net");
           
           
   
   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.com/blogsearch_feeds?hl=en&q=feedsky&ie=utf-8&num=10&output=rss";
      java.net.URLConnection feedUrl = new java.net.URL(urlStr).openConnection();
      feedUrl.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
      
      
      String urlStr1 = "http://blog.csdn.net/mhbjava/category/173484.aspx/rss";
      java.net.URLConnection feedUrl1 = new java.net.URL(urlStr1).openConnection();
      feedUrl1.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
      
      //读取某个运程的rss
      com.sun.syndication.io.SyndFeedInput input = new com.sun.syndication.io.SyndFeedInput();
      com.sun.syndication.feed.synd.SyndFeed feedone = input.build(new com.sun.syndication.io.XmlReader(feedUrl));
      
      com.sun.syndication.feed.synd.SyndFeed feedtwo = input.build(new com.sun.syndication.io.XmlReader(feedUrl1));
      
      List list = new ArrayList();
      list.addAll(feedone.getEntries());
      list.addAll(feedtwo.getEntries());
      
      feed.setEntries(list);//烧制2个rss
      
      //在控制台输出
      SyndFeedOutput output = new SyndFeedOutput();
            output.output(feed,new PrintWriter(System.out));
      
      
      
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }

}

  • src.rar (4.9 KB)
  • 下载次数: 165

你可能感兴趣的:(应用服务器,Java,SUN,Hibernate,.net)