首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 第二书店 程序员
您的位置:Web开发->RSS在Web开发过程中的全方位应用

RSS在Web开发过程中的全方位应用2008-05-21 来自:lizhe1985  [收藏到我的网摘]

来源:IT专家网

  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>

推荐人评论

rss的在java中的出现,大家对其褒贬不一,但是他毕竟是出现了,并且有很多大型的网站都在用它,不论我们是抵触也好,接受也罢,但很有可能在某一天你的老板会让你用到这项技术。我们就要具备。

用户评论

正在载入评论列表...

是谁推荐了此篇文章

专家头像李哲
个人blog发送信息
李哲推荐的其他文章

热点新闻

热点评论

    资源下载

    精彩视频