<%@ page contentType="text/html; charset=gb2312"%> rss在web开发过程中的全方位应用
网站公告:   ◆北天JAVA技术网热情为java爱好者服务,本网内容包括JAVA(JSP、servlet、EJB、webservice、j2ee、javabean、应用服务器、JavaScript),数据库(MYSQL、SQL Server、Sybase、Oracle、DB2、数据库综合知识),设计研究(设计模式、Struts、Spring、Hibernate、设计框架、设计综合知识),WEB2.0新技术(主要介绍AJAX),以及各种技术的入门、实例、例子等等,欢迎各位多来坐坐!◆  诚邀各位JAVA爱好者加盟!◆  本网站内容丰富,更新快,保证每周20篇以上!  
加入收藏
设为首页
联系站长
承接项目
  相关资源:网站首页 | 免费培训学院 | 技术论坛 | JAVA聊天室 | 作家专栏 | 开发工具 | 认证考试 | 会员俱乐部
  JAVA技术初学者园地 | jsp与servlet | javascript | Java源代码 | EJB | web service | 应用服务器 | JAVA综合知识
  设计研究设计模式 | 设计框架 | Struts | Spring | Hibernate | 开源项目 | 面向对象设计 | 设计综合知识
  数 据 库MYSQL | SQL Server | Sybase | Oracle | DB2 | Informix | Access | 数据库综合知识
  其他资源:AJAX新技术 | 网站开发 | ERP软件 | OA办公软件 | 商业智能BI | 开发综合知识 | 承接项目 | 项目试用

 
 
rss在web开发过程中的全方位应用
     发布者: 发布时间:2007-01-05
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>
(转载文章请保留出处:北天JAVA技术网(www.java114.com))
 
更多精彩文章:
一篇关于JSF的入门文章
在Red Hat Linux 9.0下安装JDK
JSP与Mysql数据库的连接
如何用Java开发"多画面浏览器" 有没有源程序?
基于Socket的Java网络编程集粹
java进度条
 
最近评论:
        
冰封的往事!
wow power leveling,wow gold,WoW Gold,wow gold max(5436)
        
飞舞的传奇!
传世私服,传世私服.传奇世界私服传奇世界私服,传世私服传世私服, 传奇世界私服传奇世界私服.传奇私服传奇私服. max(7561)
        
标 题:   
内 容:   
 
                                  
 
免责声明:该文章由网友发表,如果对您造成侵权,请联系站长

首页 - 承接项目 - 网站地图 - 联系我们 -
版权所有北天JAVA技术工作室 ICP证号:粤ICP备06079815号