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

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

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为以下同种中的一种
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文件进行读取操作。
<%@ 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
package com.cnfilm.utils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import com.cnfilm.web.film.Film; import com.sun.syndication.feed.rss.Channel; import com.sun.syndication.feed.rss.Description; import com.sun.syndication.feed.rss.Item; import com.sun.syndication.io.FeedException; import com.sun.syndication.io.WireFeedOutput; /** * 文件名:RssUtils.java 网站RSS生成 * 版本信息:V1.0 * 日期:2013-06-18 * Copyright BDVCD Corporation 2013 * 版权所有 http://www.bdvcd.com */ public class RssUtils { public static String getRssString(List filmList,HashMap map){ Channel channel = new Channel("rss_2.0"); channel.setTitle(map.get("title")); channel.setDescription(map.get("description")); channel.setLink("http://www.bdvcd.com/"); channel.setEncoding("UTF-8"); /**这个list对应rss中的item列表**/ List items = new ArrayList(); /**新建Item对象,对应rss中的**/ Item item = null; for(Film film:filmList){ item = new Item(); item.setAuthor(film.getStr("starring")); item.setLink("http://www.bdvcd.com/"+film.getStr("curl")+"/"+film.getStr("url")+".html"); item.setPubDate(DateUtils.parse(film.getStr("addtime"))); item.setTitle(film.getStr("fname")); Description description = new Description(); description.setValue(film.getStr("content")); item.setDescription(description); items.add(item); } channel.setItems(items); /**用WireFeedOutput对象输出rss文本**/ WireFeedOutput out = new WireFeedOutput(); String rssString = ""; try { rssString = out.outputString(channel); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (FeedException e) { e.printStackTrace(); } return rssString; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值