JR 精品文章 - rss在web开发过程中的全方位应用
AD: jr (at) javaresearch.org


首页 | 动态 | 文章 | FAQ  | 新闻 | 下载 | 代码 | 工作 | 调查 | 术语 | 站点 | 图书 | 论坛 | 帮助 | 全部  

TOP | 交流 | 软件 | 专栏 | 开源 | 译/著 | 源码 | API  | 推荐 | FTP  | 积分 | 统计 | 搜索 | Blog | 我们  
首页 » 研究文集 » JSP/Servlet/JSF 搜索标题相关文章 搜索标题相关文章    评论此文章 发表评论     开始监控此文章 开始监控   加入收藏夹  加入收藏夹
rss在web开发过程中的全方位应用
wwty 整理   更新:2006-12-13 10:41:02  版本: 1.0   

rss的在java中的出现,大家对其褒贬不一,但是他毕竟是出现了,并且有很多大型的网站都在用它,不论我们是抵触也好,接受也罢,但很有可能在某一天你的老板会让你用到这项技术。我们就要具备。
在web当中应用,要有以下两个开发包,
jdom.jar:JDOM开源项目中(http://www.jdom.org/
rome.jar : ROME开源项目中(http://wiki.java.net/bin/view/Javawsxml/Rome
关于rss在javaweb中的应用,分为两个步骤实施:
1.生成rss格式的xml文件:
public class FeedWriter {

    private static final String DATE_FORMAT = "yyyy-MM-dd";

    public static void main(String[] args) {
        boolean ok = false;
        if (args.length==2) {
            try {
                String feedType = args[0];
                String fileName = args[1];

                DateFormat dateParser = new SimpleDateFormat(DATE_FORMAT);

                SyndFeed feed = new SyndFeedImpl();  //feed流
                feed.setFeedType(feedType);                   //设置rss版本

                feed.setTitle("Sample Feed (created with Rome)");  //<title>设置标题
                feed.setLink(http://www.csdn.net);                               //<link>
                feed.setDescription("This feed has been created using Rome (Java syndication utilities");

                List entries = new ArrayList();
                SyndEntry entry;
                SyndContent description;

                entry = new SyndEntryImpl();                                 //子节点
                entry.setTitle("Rome v1.0");
                entry.setLink(http://www.csdn.net);
                entry.setPublishedDate(dateParser.parse("2004-06-08"));
                description = new SyndContentImpl();
                description.setType("text/plain");
                description.setValue("Initial release of Rome");
                entry.setDescription(description);
                entries.add(entry);

                entry = new SyndEntryImpl();
                entry.setTitle("Rome v2.0");
                entry.setLink(http://ww.csdn.net);
                entry.setPublishedDate(dateParser.parse("2004-06-16"));
                description = new SyndContentImpl();            //描述
                description.setType("text/xml");
                description.setValue("Bug fixes, <xml>XML</xml> minor API changes and some new features");
                entry.setDescription(description);
                entries.add(entry);

                entry = new SyndEntryImpl();
                entry.setTitle("Rome v3.0");
                entry.setLink("http://www.csdn.net");
                entry.setPublishedDate(dateParser.parse("2004-07-27"));
                description = new SyndContentImpl();
                description.setType("text/html");
                description.setValue("<p>More Bug fixes, mor API changes, some new features and some Unit testing</p>"+
                                     "<p>For details check the <a href=\"http://www.csdn.net\">Changes Log</a></p>");
                entry.setDescription(description);
                entries.add(entry);

                feed.setEntries(entries);                             //设置子节点

                Writer writer = new FileWriter(fileName);
                SyndFeedOutput output = new SyndFeedOutput();
                output.output(feed,writer);                        //写到文件中去
              
                writer.close();

                System.out.println("The feed has been written to the file ["+fileName+"]");

                ok = true;
            }
            catch (Exception ex) {
                ex.printStackTrace();
                System.out.println("ERROR: "+ex.getMessage());
            }
        }

        if (!ok) {
            System.out.println();
            System.out.println("FeedWriter creates a RSS/Atom feed and writes it to a file.");
            System.out.println("The first parameter must be the syndication format for the feed");
            System.out.println("  (rss_0.90, rss_0.91, rss_0.92, rss_0.93, rss_0.94, rss_1.0 rss_2.0 or atom_0.3)");
            System.out.println("The second parameter must be the file name for the feed");
            System.out.println();
        }
    }

}
2.对此xml文件的读取:

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sina News</title>
</head>
<body>

<%
java.util.Properties systemSettings = System.getProperties();
systemSettings.put("http.proxyHost", "mywebcache.com");
systemSettings.put("http.proxyPort", "8080");
System.setProperties(systemSettings);

String urlStr = "http://rss.sina.com.cn/news/marquee/ddt.xml";
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));
%>

<div align="center">
<h1><%=feed.getTitle()%></h1>
<table border=1 cellpadding=3 width="700">
<tr>
<th>Number</th>
<th>Title</th>
<th>Time</th>
</tr>

<%
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);
%>

<tr>
<td><%=i+1%></td>
<td><a href="<%=entry.getLink()%>"><%=entry.getTitle()%></a></td>
<td><%=entry.getPublishedDate()%></td>
</tr>
<%}%>

</table>
</div>
<br>
</body>
</html>




版权声明  
本篇文章对您是否有帮助?  投票:         投票结果:     1       0
作者其它文章: 作者全部文章
评论人:vampire_315 发表时间: Sun Dec 17 20:25:11 CST 2006
好啊
评论人:bruceel_2006 发表时间: Wed Dec 20 14:38:50 CST 2006
hao

这个文章共有 2 条评论
主题: ServletContext与ServletConfig的深度分析 上一篇文章
返回文章列表 返回〔JSP/Servlet/JSF〕
下一篇文章 主题: 验证码机制实现初探


文字广告链接
        自主、快速定制基于JAVA的B/S业务系统          重量级企业在线自定义WEB报表平台
        Excel制表、零代码发布、打印、图表结合——快逸报表,免费、稳定、功能强大的java工具
        技术圈: 关于Java、dotNet、PHP、Ruby、奇客、Web2.0等更多资讯博客精选文章

关于 JR  |  版权声明  |  联系我们 

©2002-2006 JR 版权所有 沪ICP备05019622号