RSS读取与生成实现原理

package com.csst.ebt.app;
import java.io.StringReader;
import java.util.List;

import net.sf.json.JSONArray;

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 {
	    
	    /**
	     * 读取rss的信息
	     * @param context
	     * @throws ParseException
	     */
		public 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();			
			//此处我们紧紧关注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();
			  }
	        }
		}

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

}


 

你可能感兴趣的:(RSS读取与生成实现原理)