laoding
          本來我以為,隱身了別人就找不到我,沒有用的,像我這樣拉風的男人,無論走到哪里,都像在黑暗中的螢火蟲一樣,那樣的鮮明,那樣的出眾。我那憂郁的眼神,稀疏的胡茬,那微微隆起的將軍肚和親切的笑容......都深深吸引了眾人......
          posts - 0,  comments - 37,  trackbacks - 0

          自己去下載jar包,rsslib4j-0.2.jar,rome-0.8.jar,jdom.jar。

          表newitem如下(自己參考著建):

          CREATE TABLE `newitem` (                    
                     `id` 
          varchar(20NOT NULL,                
                     `title` 
          varchar(30default NULL,         
                     `link` 
          varchar(100default NULL,         
                     `description` 
          varchar(100default NULL,  
                     `pubdate` date 
          default NULL,              
                     `category` 
          varchar(30default NULL,      
                     `author` 
          varchar(30default NULL,        
                     `enclosure` 
          varchar(50default NULL,     
                     
          PRIMARY KEY  (`id`)                       
                   )

          下面是幾個要用到類

          import java.util.Date;

          public class ChannelItem {
              
              
          private String title;// Rss文件中Item的標題

              
          private String link;// Rss文件中Item對應的連接

              
          private String description;// Item的描述

              
          private Date pubDate;// Item發布的時間

              
          private String author;// Item作者

              
          private String category;// Item所屬的頻道范疇

              
          public String getTitle() {
                  
          return title;
              }

              
          public void setTitle(String title) {
                  
          this.title = title;
              }

              
          public String getLink() {
                  
          return link;
              }

              
          public void setLink(String link) {
                  
          this.link = link;
              }

              
          public String getDescription() {
                  
          return description;
              }

              
          public void setDescription(String description) {
                  
          this.description = description;
              }

              
          public Date getPubDate() {
                  
          return pubDate;
              }

              
          public void setPubDate(Date pubDate) {
                  
          this.pubDate = pubDate;
              }

              
          public String getAuthor() {
                  
          return author;
              }

              
          public void setAuthor(String author) {
                  
          this.author = author;
              }

              
          public String getCategory() {
                  
          return category;
              }

              
          public void setCategory(String category) {
                  
          this.category = category;
              }
          }


          public class ChannelEItem extends ChannelItem {
              
              
          private String enclosure;// 流媒體文件

              
          public String getEnclosure() {
                  
          return enclosure;
              }

              
          public void setEnclosure(String enclosure) {
                  
          this.enclosure = enclosure;
              }
          }


          import java.io.FileOutputStream;
          import java.io.OutputStreamWriter;
          import java.io.Writer;
          import java.util.ArrayList;
          import java.util.Date;
          import java.util.List;

          import com.sun.syndication.feed.synd.SyndCategory;
          import com.sun.syndication.feed.synd.SyndCategoryImpl;
          import com.sun.syndication.feed.synd.SyndContent;
          import com.sun.syndication.feed.synd.SyndContentImpl;
          import com.sun.syndication.feed.synd.SyndEnclosure;
          import com.sun.syndication.feed.synd.SyndEnclosureImpl;
          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 RssBuildFactory {
              
          private SyndFeed feed;

              @SuppressWarnings(
          "unchecked")
              
          private List entries;

              
          private SyndEntry entry;

              @SuppressWarnings(
          "unchecked")
              
          public RssBuildFactory() {
                  feed 
          = new SyndFeedImpl();
                  feed.setFeedType(
          "rss_2.0");
                  entries 
          = new ArrayList();
              }

              
          /**
               * 創建一個頻道
               * 
               * 
          @param title
               *            頻道標題
               * 
          @param link
               *            頻道對應的連接
               * 
          @param description
               *            頻道描述
               * 
          @param language
               *            頻道所用語言
               * 
          @param pubDate
               *            頻道發布時期
               * 
          @param copyright
               *            版權所有
               * 
          @throws Exception
               
          */
              
          public void buildChannel(String title, String link, String description,
                      String language, Date pubDate, String copyright)
                      
          throws RuntimeException {
                  feed.setTitle(title);
                  feed.setLink(link);
                  feed.setDescription(description);
                  feed.setLanguage(language);
                  feed.setPublishedDate(pubDate);
                  feed.setCopyright(copyright);
              }

              
          /**
               * 添加頻道的子內容
               * 
               * 
          @param item
               *            {
          @link ChannelItem}
               * 
          @throws Exception
               
          */
              @SuppressWarnings(
          "unchecked")
              
          public void buildItems(ChannelItem item) throws RuntimeException {
                  entry 
          = new SyndEntryImpl();
                  
          // 設置新聞標題
                  entry.setTitle(item.getTitle());
                  
          // 設置新聞的連接地址
                  entry.setLink(item.getLink());
                  
          // 設置新聞簡介
                  SyndContent content = new SyndContentImpl();
                  content.setType(
          "text/plain");
                  content.setValue(item.getDescription());
                  entry.setDescription(content);
                  
          // 設置發布時間
                  entry.setPublishedDate(item.getPubDate());
                  
          // 設置頻道所屬的范圍
                  SyndCategory cate = new SyndCategoryImpl();
                  cate.setName(item.getCategory());
                  List cateList 
          = new ArrayList();
                  cateList.add(cate);
                  entry.setCategories(cateList);
                  
          // 設置作者
                  entry.setAuthor(item.getAuthor());
                  
          // 將新聞項添加至數組中
                  entries.add(entry);
              }

              
          /**
               * 添加頻道的內容項
               * 
               * 
          @param item
               *            {
          @link ChannelEItem}此類繼承自ChannelItem類
               * 
          @throws Exception
               
          */
              @SuppressWarnings(
          "unchecked")
              
          public void buildItems(ChannelEItem item) throws RuntimeException {
                  entry 
          = new SyndEntryImpl();
                  
          // 設置新聞標題
                  entry.setTitle(item.getTitle());
                  
          // 設置新聞的連接地址
                  entry.setLink(item.getLink());
                  
          // 設置新聞簡介
                  SyndContent content = new SyndContentImpl();
                  content.setValue(item.getDescription());
                  entry.setDescription(content);
                  
          // 設置發布時間
                  entry.setPublishedDate(item.getPubDate());
                  
          // 設置頻道所屬的范圍
                  SyndCategory cate = new SyndCategoryImpl();
                  cate.setName(item.getCategory());
                  List cateList 
          = new ArrayList();
                  cateList.add(cate);
                  entry.setCategories(cateList);
                  
          // 設置作者
                  entry.setAuthor(item.getAuthor());
                  
          // 設置流媒體播放文件
                  SyndEnclosure en = new SyndEnclosureImpl();
                  en.setUrl(item.getEnclosure());
                  List enList 
          = new ArrayList();
                  enList.add(en);
                  entry.setEnclosures(enList);
                  
          // 將新聞項添加至數組中
                  entries.add(entry);
              }

              
          /**
               * 生成XML文件
               * 
               * 
          @param filePath
               *            文件保存路徑和名稱
               * 
          @throws Exception
               
          */
              
          public void buildChannel(String filePath) throws Exception {
                  feed.setEntries(entries);
                  SyndFeedOutput output 
          = new SyndFeedOutput();
                  Writer writer;
                  writer 
          = new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8");
                  output.output(feed, writer);
              }
          }

          public class DBConnection {

              
          public static void main(String[] args) {


                  DBConnection db 
          = new DBConnection();
                  Connection conn 
          = db.getConnection();
                  String sql 
          = "select * from newitem";
                  ResultSet rs 
          = db.getResultSet(sql, conn);
                  
          try {
                      
          while (rs.next()) { 
                          String title 
          = rs.getString("title");
                          System.out.println(title); 
                       }
                  } 
          catch (SQLException e) {
                  
                      e.printStackTrace();
                  } 
              
              }

              
          public Connection getConnection() {
                  Connection conn 
          = null;
                  
          try {
                      Class.forName(
          "com.mysql.jdbc.Driver").newInstance();

                      
          // 建立到MySQL的連接
                      conn = DriverManager.getConnection(
                              
          "jdbc:mysql://localhost:3306/ding""root""ding");
                  } 
          catch (Exception e) {
                      System.out.println(
          "數據庫連接異常!");
                      e.printStackTrace();
                  }
                  
          return conn;
              }

              
          public ResultSet getResultSet(String sql, Connection conn) {
                  ResultSet rs 
          = null;
                  Statement stmt 
          = null;
                  
          try {
                      stmt 
          = conn.createStatement();
                      rs 
          = stmt.executeQuery(sql);
                  } 
          catch (Exception e) {
                      e.printStackTrace();
                  }
                  
          return rs;
              }
          }

          最后這個是測試類

          public class TestMain {

              
          /**
               * @Description 方法實現功能描述
               * 
          @param args
               *            void
               * 
          @throws 拋出異常說明
               
          */
              
          public static void main(String[] args) {
                  
          new TestMain().testBuildObject();
              }

              
          public void testBuildObject() {
                  
          try {

                      
          // 建立數據庫的連接
                      DBConnection db = new DBConnection();

                      
          // 查詢Sql語句
                      String querySql = "select * from newitem";
                      Connection conn 
          = db.getConnection();
              
                      ResultSet rs 
          = db.getResultSet(querySql, conn);

                      
          // 建立Rss生成器工廠
                      RssBuildFactory builder = new RssBuildFactory();

                      
          // 循環遍歷數據庫記錄生成Rss中的Item項
                      while (rs.next()) {
                          ChannelEItem item 
          = new ChannelEItem();
                          item.setTitle(rs.getString(
          "title"));
                          item.setLink(rs.getString(
          "link"));
                          item.setDescription(rs.getString(
          "description"));
                          item.setPubDate(rs.getDate(
          "pubdate"));
                          item.setCategory(rs.getString(
          "category"));
                          item.setAuthor(rs.getString(
          "author"));
                          item.setEnclosure(rs.getString(
          "enclosure"));
                          builder.buildItems(item);
                      }

                      
          // 建立Rss的Channel信息
                      builder.buildChannel("laoding的測試",
                              
          "http://www.aygfsteel.com/laoding/""測試生成""zh-cn",
                              
          new Date(), "老丁");

                      
          // 設置Rss文件的生成路徑
                      builder.buildChannel("demo.xml");
                      System.out.println(
          "create rss xml success!!");
                  } 
          catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          }

          執行這個測試類,控制臺輸出:

          create rss xml success!!

          表示成功,生成的demo.xml文件如下:
          <?xml version="1.0" encoding="UTF-8"?>
          <rss xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
            
          <channel>
              
          <title>laoding的測試</title>
              
          <link>http://www.aygfsteel.com/laoding/</link>
              
          <description>測試生成</description>
              
          <language>zh-cn</language>
              
          <copyright>老丁</copyright>
              
          <pubDate>Wed, 22 Oct 2008 09:25:56 GMT</pubDate>
              
          <dc:date>2008-10-22T09:25:56Z</dc:date>
              
          <dc:language>zh-cn</dc:language>
              
          <dc:rights>老丁</dc:rights>
              
          <item>
                
          <title>title1</title>
                
          <link>http://www.aygfsteel.com/laoding</link>
                
          <description>laoding's javablog</description>
                
          <enclosure url="111" />
                
          <category>1111</category>
                
          <guid>http://www.aygfsteel.com/laoding</guid>
                
          <dc:creator>laoding</dc:creator>
              
          </item>
              
          <item>
                
          <title>title2</title>
                
          <link>http://www.aygfsteel.com/laoding</link>
                
          <description>laoding's javablog</description>
                
          <enclosure url="222" />
                
          <category>2222</category>
                
          <guid>http://www.aygfsteel.com/laoding</guid>
                
          <dc:creator>laoding</dc:creator>
              
          </item>
              
          <item>
                
          <title>333</title>
                
          <link>33333</link>
                
          <description>3333</description>
                
          <enclosure url="333" />
                
          <category>333</category>
                
          <guid>33333</guid>
                
          <dc:creator>33</dc:creator>
              
          </item>
              
          <item>
                
          <title>444</title>
                
          <link>44444</link>
                
          <description>44444</description>
                
          <enclosure url="4444" />
                
          <category>4444</category>
                
          <guid>44444</guid>
                
          <dc:creator>4444</dc:creator>
              
          </item>
            
          </channel>
          </rss>


          這個xml文件就是符合rss格式的,發布到web工程中就可以被RSS閱讀器訂閱了,嘿嘿

          具體的應用就要靠自己去擴展了,有疑問請留言。



          posted on 2008-10-22 17:29 老丁 閱讀(1033) 評論(3)  編輯  收藏 所屬分類: RSS聚合

          FeedBack:
          # re: 創建自己的rss
          2008-10-25 22:28 | Heinvo Lee
          I appriciate your article about RSS! I'll read it in detail later...  回復  更多評論
            
          # re: 創建自己的rss[未登錄]
          2009-03-16 17:08 | snow
          到哪下載呀,在網上搜了N久找不到。  回復  更多評論
            
          # re: 創建自己的rss[未登錄]
          2009-03-16 19:33 | 老丁
          @snow
          把你的郵箱告訴我,我發給你,你是要jar包么?  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          本博客主為學習和復習之用,無關其他,想罵人的繞道
          Email:dkm123456@126.com
          大家一起交流進步
          QQ:283582761


          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          留言簿(4)

          我參與的團隊

          文章分類(50)

          文章檔案(48)

          相冊

          朋友

          搜索

          •  

          積分與排名

          • 積分 - 96532
          • 排名 - 600

          最新評論

          主站蜘蛛池模板: 津市市| 荆门市| 蒲江县| 裕民县| 攀枝花市| 达日县| 彭山县| 巴林右旗| 惠水县| 南皮县| 阿尔山市| 滕州市| 广平县| 原平市| 武山县| 隆化县| 巩留县| 正宁县| 雷州市| 儋州市| 鄂尔多斯市| 麻江县| 宝应县| 吉木萨尔县| 三河市| 铁岭市| 阳高县| 东平县| 永兴县| 叶城县| 高安市| 习水县| 八宿县| 蒙自县| 堆龙德庆县| 海林市| 龙游县| 汝南县| 安远县| 上饶市| 车致|