采用Rsslibj的生成和解析RSS1.0

RSS是一个标准的XML文件,Rss阅读器可以读取这个XML文件获得文章的信息,使用户可以通过Rss阅读器

而非浏览器阅读Blog,我们只要动态生成这个XML文件便可以了。RSSLibJ是一个专门读取和生成RSS的小

巧实用的Java库,大小仅25k,可以从http://sourceforge.net/projects/rsslibj/下载rsslibj-

1_0RC2.jar和它需要的EXMLjar两个文件,然后复制到web/WEB-INF/lib/下。

  

rsslibj-1_0RC2.jar下载地址:http://sourceforge.net/project/downloading.php?group_id=71153&use_mirror=nchc&filename=rsslibj-1_0RC2.jar&27763931

  

EXML.jar下载地址:http://rsslibj.cvs.sourceforge.net/rsslibj/rsslibj/lib/EXML.jar?view=log

package com.easyway.rss.app;
import java.io.StringReader;
import java.util.List;

import com.rsslibj.elements.Channel;
import com.rsslibj.elements.Item;
import com.rsslibj.elements.RSSReader;

import electric.xml.ParseException;
/***
 * rss是什么?     
 *     rss是在线共享内容的一种简易方式(也叫聚合内容,Really Simple Syndication 的简称,通常在
 *时效性比较强的内容上使用RSS订阅能更快速获取信息,网站提供RSS输出,有利于让用户获取网站内容的最
 *新更新,其实是一种能跨平台的服务于用户的简单xml文件协议.
 *
 *为什么要RSS?
 *              在这个网络知识繁华的岁月,每个人都希望能快速的知道自己关心的知识,比如我关心x站的x
 * 一个栏目,我就可以订阅该栏目的rss,如有文章发布,我就能及时的看到更新...中间的商机我就不再赘言了.
 * 
 * 
 * 目前网上的使用主要分为以下几种:
 * 1.不同网站中blog的迁移
 *    如ITEye的blog到ITpub blog数据的迁移
 * 2.订阅特定网站的信息的更新
 *    如果订阅特定的网站的blog更新
 * 3.RSS在线阅读器的使用。
 *    通过手机等通讯工具在线阅读最新信息
 * 
 * 
 * 
 * RSS文件结构
 * 以下以RSS2.0为例说明.rss文件的核心就是xml文件,所以首先必须符合xml的构架格式.
 *  它是以
 *<rss version="2.0">...</rss>
 *这种Root形式的格式.
 *       rss有一<channel>的子节点,它包含了文件的内容,在<channel>的里面,有好几个元素用以描述信息.
 * 在站点http://backend.userland.com/rss上有详细的内容,比如以下:
 *  title:标题,经常还有资料的来源信息
 *  link:web站点的url地址
 * description:对网站的一个简单描述.
 *  每条信息用以<item>元素表示,它被包含在<channel>节点里面,每个<channel>可以有多个<item>,每个<item>节点是真正的节点信息:
 *  title:列表项目的标题
 * link:列表项目的web url地址,
 * description:对列表项目的简短说明,
 * author:列表信息的作者
 * pubDate:发布时间.
 *  这里,有一个很重要的节点就是pubDate的格式,它必须符合RFC 822的标准,查看细节 .开始于三个字母长度的星期,然后是每月的
 *    天数次序,然后是3个字母的月份,然后是年份,然后是具体的时间,最后是时区.
 * 
 * 
 * @Title: RSSLibJ的使用
 * @Description: 使用RSSLibJ生成相关的RSS文件
 * @Copyright:Copyright (c) 2011
 * @Company:易程科技股份有限公司
 * @Date:2012-2-6
 * @author longgangbai
 * @version 1.0
 */
public class RSSLibJMain {
	    public static void main(String[] args) 
	            throws InstantiationException, ClassNotFoundException, 
	            IllegalAccessException{
	        try {
	          //网络获取RSS的方式
	          // URL url=new URL("http://topmanopensource.iteye.com/rss");
	  	      // InputStream inputstream=url.openStream();
	          // BufferedReader reader= new BufferedReader(new InputStreamReader(inputstream));
	        	
	        	//获取自定义的RSS格式(备注自定义的RSS版本为V1.0)
	        	String context=writerRSS();
	        	readerRSS(context);
			} catch (Exception e) {
				e.printStackTrace();
			}
	      
	    }

	    /**
	     * 读取rss的信息
	     * @param context
	     * @throws ParseException
	     */
		private static void readerRSS(String context) throws ParseException {
			StringReader reader=new StringReader(context);
			//创建RSS阅读对象的
			RSSReader rssReader=new RSSReader();
			//设置读取内容
			rssReader.setReader(reader);
			//获取rss相关的信息
			Channel channel=rssReader.getChannel();
			String description=channel.getDescription();
			System.out.println("description="+description);
			String link=channel.getLink();
			System.out.println("link="+link);
			//此处我们紧紧关注item
			List<Item> itemList=channel.getItems();
	      if(itemList!=null){
			  for (Item item : itemList) {
				  String slink=item.getLink();
				  String title=item.getTitle();
				  String author=item.getDcContributor()==null?item.getDcCreator():item.getDcContributor();
				  String descption=item.getDescription();
				 
				  System.out.println(title +" "+slink +" "+author +" "+descption);
			  }
	      }
		}

	    /**
	     * 创建一个自定义的RSS的
	     * @return
	     * @throws InstantiationException
	     * @throws IllegalAccessException
	     * @throws ClassNotFoundException
	     */
		private static String writerRSS() throws InstantiationException,
				IllegalAccessException, ClassNotFoundException {
			Channel channel=new Channel();
	        channel.setDescription("This is my sample channel.");
	        channel.setLink("http://localhost/");
	        channel.setTitle("My Channel");
	        channel.setImage("http://localhost/", 
	                "The Channel Image", 
	                "http://localhost/foo.jpg");
	        channel.setTextInput("http://localhost/search", 
	                "Search The Channel Image", 
	                "The Channel Image", 
	                "s");
	        channel.addItem("http://localhost/item1",
	                "The First Item covers details on the first item>",
	                "The First Item")
	                .setDcContributor("Joseph B. Ottinger");
	        channel.addItem("http://localhost/item2",
	                "The Second Item covers details on the second item",
	                "The Second Item")
	                .setDcCreator("Jason Bell");
	        System.out.println("The feed in RDF: ");
	        
	        System.out.println(channel.getFeed("rdf"));
	        
	        return channel.getFeed("rdf");
		}

}

猜你喜欢

转载自topmanopensource.iteye.com/blog/1396548
今日推荐