rss在web開發過程中的全方位應用

maqianmaqian發表於2010-10-09

      http://www.qqvan.com/item/ri6

 

      簡介:這是rss在web開發過程中的全方位應用。的詳細頁面,介紹了和java,.net,rss,csdn,sun,entry,有關的知識,加入收藏請按鍵盤ctrl+D,謝謝大家的觀看!要檢視更多有關資訊,請點選此處

 在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 {

和 "rss在web開發過程中的全方位應用。" 有關的java程式設計小帖士:

strong>Locale.equals

比較當前Locale物件和作為引數傳入的Locale物件。

語法

public boolean equals ( Object obj );

引數

obj

與當前Locale物件相比較的物件。

返回值

如果兩個Locale物件合理地相等(logically equivalent),則返回true,否則返回false。

說明

用這個方法比較當前Locale物件和作為引數傳入的Locale物件。如果兩個Locale物件描述的是同一個場所,則返回true。

  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>

相關文章