rsslibj生成rss2.0搞不定,换了rome0.8来试试

  在rsslibj中生成rss2.0用getFeed("2.0")用下载的binary不行,用cvs库中的文件更不行,老是报告nullpoint错误,而且,文档和源程序混乱。

  试用了rome,感觉更正规一些,文档丰富,设计上好像要全面一些。毕竟,它是后来者,而rsslibj在2003年就停止发布了。

  rome自身带的例子已经比较全面,可以来它来写出基本功能的rss文件,下面附网上一篇完整的例子:

转自:http://kb.csdn.net/java/Articles/200512/10b9356d-85cd-4fe0-b8ae-3eece6cdb928.html

How To Generate RSS With ROME

作者: ∣来源:JavaLobby∣原文地址∣2005-12-30

Many people have asked how I implemented RSS in Fatima (JavaPress) on Java.net since the object model for Suns Rome API is a little.. dense, and suited more for abstract aggregation in your applications like Technorati. However, Rome is extremely powerful and offers lots of transformation and organization of your RSS feeds (no matter if you generate them or you grab them). Well, below is a general class that implements everything you would need to start generating your own RSS feeds using the ROME API, and for everyone that wants to get started quick this example should help you out.

doSyndication() is where youll want to focus your attention.

NOTE: This generates both regular RSS and shows you how to generate RSS with the embed tag for Podcasts!

package net.thepostmodern.fatima.rss;

 

import com.sun.syndication.feed.synd.*;

import com.sun.syndication.io.SyndFeedOutput;

import java.io.FileWriter;

import java.io.Writer;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.List;

import java.sql.*;

 

import net.thepostmodern.fatima.blog.*;

import net.thepostmodern.fatima.sql.ConnectionFactory;

 

 

/**

 * @author   bbjwerner

 */

public class GenerateRSS {

private static final DateFormat DATE_PARSER = new SimpleDateFormat("yyyy-MM-dd");

private static String fileName = null;

private static String feedType = null;

    private final List entries = new ArrayList();

private final List categories = new ArrayList();

    SyndEntry entry;

    SyndContent description;

SyndCategory category;

 

/**

* This method generates a seperate RSS feed for all categories in blog

* @param feedType The type of feed you wish to generate (RSS2.0, atom, ect.).

* @param fileName Filename will be category.xml, and this entry does nothing to override this.

*/

public void generateCategoryRSS(String Type, String Name) {

feedType = Type;

fileName = Name;

populateCategoryArray();

}

/**

* This method generates the RSS feeds for a complete blog with categories

* @param Type The type of feed you wish to generate (RSS2.0, atom, ect.).

* @param Name The filename to write out the format to.

*/

public void generateBlogRSS(String Type, String Name) {

feedType = Type;

fileName = Name;

this.populateArray();

}

       

        /**

* This method generates the RSS feeds for podcasting based on category (i.e. Podcast)

* @param Category The category to run as containing podcast content.

* @param Name The filename to write out the format to.

*/

        public void generatePodcastRSS(String Category, String Name) {

feedType = "rss2.0";

fileName = Name;

this.populatePodcastArray(Category);

}

public GenerateRSS() {

   

}

private void populateArray() {

 

    final ConnectionFactory thisFactory = new ConnectionFactory();

ResultSet blogEntries = null;

ResultSet blogInfo = null;

    try {

blogEntries = thisFactory.executeStatement("SELECT * FROM blog_entry ORDER BY id DESC");

blogInfo = thisFactory.executeStatement("SELECT * FROM blog_info");

blogEntries.first();

blogInfo.first();

while (!blogEntries.isAfterLast()) {

   

  this.addEntry(blogEntries.getString("title"),blogInfo.getString("url") + "/blog/BlogEntry.jsp?id=" + blogEntries.getString("id"), blogEntries.getString("date"), blogEntries.getString("teaser"), CategoryBean.getCategory(blogEntries.getString("category")), blogEntries.getString("author"));

  blogEntries.next();

}

this.doSyndication(blogInfo.getString("name"), blogInfo.getString("url") + "/blog", blogInfo.getString("description"), blogInfo.getString("copyright"),fileName);

System.out.println("Did Syndication");

}

catch (Exception ex) {

System.out.println("SQLException: " + ex.getMessage());

}

finally {

   

    try {blogEntries.close(); } catch (Exception ex) { System.out.println("SQLException: " + ex.getMessage()); }

    try {blogInfo.close(); } catch (Exception ex) { System.out.println("SQLException: " + ex.getMessage()); }

    thisFactory.Close();

}

}

private void populateCategoryArray() {

 

    final ConnectionFactory thisFactory = new ConnectionFactory();

ResultSet blogEntries = null;

ResultSet blogInfo = null;

ResultSet blogCategories = null;

try {

blogCategories = thisFactory.executeStatement("SELECT * FROM blog_categories");

blogInfo = thisFactory.executeStatement("SELECT * FROM blog_info");

blogInfo.first();

blogCategories.first();

while (!blogCategories.isAfterLast()) {

   

    blogEntries = thisFactory.executeStatement("SELECT * FROM blog_entry WHERE category = '" + blogCategories.getString("id") + "' ORDER BY id DESC");

blogEntries.first();

System.out.println("************ Doing category: " + CategoryBean.getCategory(blogCategories.getString("id")));

while (!blogEntries.isAfterLast()) { 

  this.addEntry(blogEntries.getString("title"),blogInfo.getString("url") + "/blog/BlogEntry.jsp?id=" + blogEntries.getString("id"), blogEntries.getString("date"), blogEntries.getString("teaser"), CategoryBean.getCategory(blogEntries.getString("category")), blogEntries.getString("author"));

  blogEntries.next();

}

final String XMLfile = ((fileName + blogCategories.getString("category")) + ".xml");

this.doSyndication(blogCategories.getString("title"), blogInfo.getString("url") + "/blog/BlogbyCategory.jsp?id=" + blogCategories.getString("id"), blogCategories.getString("description"), blogInfo.getString("copyright"), XMLfile);

this.entries.clear();

blogCategories.next();

}

}

catch (Exception ex) {

System.out.println("Category SQLException: " + ex.getMessage());

}

finally {

   

    try {blogEntries.close(); } catch (Exception ex) { System.out.println("SQLException: " + ex.getMessage()); }

    try {blogInfo.close(); } catch (Exception ex) { System.out.println("SQLException: " + ex.getMessage()); }

    try {blogCategories.close(); } catch (Exception ex) { System.out.println("SQLException: " + ex.getMessage()); }

    thisFactory.Close();

}

}

private void populatePodcastArray(String category_string) {

   

    final ConnectionFactory thisFactory = new ConnectionFactory();

ResultSet podEntries = null;

ResultSet blogInfo = null;

    try {

    podEntries = thisFactory.executeStatement("SELECT * FROM podcasts");

blogInfo = thisFactory.executeStatement("SELECT * FROM blog_info");

podEntries.first();

blogInfo.first();

while (!podEntries.isAfterLast()) {

   

  this.addPodCast(podEntries.getString("title"),blogInfo.getString("url") + "/blog/radio/" + podEntries.getString("filename"),podEntries.getString("date"), podEntries.getString("description"), category_string, podEntries.getString("author"));

  podEntries.next();

}

this.doSyndication(blogInfo.getString("name"), blogInfo.getString("url"), blogInfo.getString("description"), blogInfo.getString("copyright"),fileName);

System.out.println("Did Syndication");

}

catch (Exception ex) {

System.out.println("SQLException: " + ex.getMessage());

}

finally {

   

    try {podEntries.close(); } catch (Exception ex) { System.out.println("SQLException: " + ex.getMessage()); }

    try {blogInfo.close(); } catch (Exception ex) { System.out.println("SQLException: " + ex.getMessage()); }

    thisFactory.Close();

}

}

 

/**

* This method adds an entry to the ArrayList() which will be published when GenerateRSS()

* is called.

* <p>

* @param title The title of the blog entry (not the blog itself)

* @param link The PermaLink that will point to your entry

* @param date The date of the blog entry

* @param blogContent The content or synopsis you wish to publish

* @param cat The category of the entry. This has been added to integrate

*        with Technorati and match WordPress functionality

* @param author The author of the entry to be published.

*

*/

private void addEntry(String title, String link, String date, String blogContent, String cat, String author) {

try {

 

        entry = new SyndEntryImpl();

    entry.setAuthor(author);

        entry.setTitle(title);

        entry.setLink(link);

        entry.setPublishedDate(DATE_PARSER.parse(date));

        description = new SyndContentImpl();

        description.setType("text/plain");

        description.setValue(blogContent);

        entry.setDescription(description);

    category = new SyndCategoryImpl();

category.setName(cat);

categories.add(category);

entry.setCategories(categories);

categories.remove(category);

entries.add(entry);

}

        catch (Exception ex) {

            ex.printStackTrace();

            System.out.println("ERROR: "+ex.getMessage());

                             }

}

private void addPodCast(String title, String mp3link, String date, String blogContent, String cat, String author) {

try {

 

        entry = new SyndEntryImpl();

entry.setAuthor(author);

        entry.setTitle(title);

        entry.setLink(mp3link);

        entry.setPublishedDate(DATE_PARSER.parse(date));

        description = new SyndContentImpl();

        description.setType("text/plain");

        description.setValue(blogContent);

        entry.setDescription(description);

    category = new SyndCategoryImpl();

category.setName(cat);

categories.add(category);

entry.setCategories(categories);

categories.remove(category);

entries.add(entry);

}

        catch (Exception ex) {

            ex.printStackTrace();

            System.out.println("ERROR: "+ex.getMessage());

                             }

}

/**

* This method is called last after you have added all your entries and have specified your

* feed type and filename. This actually does the work

* <p>

* NOTE: This has static content entered in to the fields! You must have access to the source

* code edit this method or else you will be publishing content as the Post Modern Banter Blog

* Yes, I should change this immediately. Ideally, it would take values from the web.xml file itself.

* <p>

* @throws Exception

*/

private void doSyndication(String title, String link, String description_loc, String copyright, String xml) {

 

            try {

 

                final SyndFeed feed = new SyndFeedImpl();

                feed.setFeedType(feedType);

 

                feed.setTitle(title);

                feed.setLink(link);

                feed.setDescription(description_loc);

   feed.setCopyright(copyright);

                feed.setEntries(entries);

  

 

                final Writer writer = new FileWriter(xml);

                final SyndFeedOutput output = new SyndFeedOutput();

                output.output(feed,writer);

                writer.close();

 

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

 

            }

            catch (Exception ex) {

                ex.printStackTrace();

                System.out.println("ERROR: "+ex.getMessage());

                                 }

       

}

 

}

--------------------------------------------------------------------------------

作者的其他文章

Atlas 实现机制浅析 [3]

Atlas 实现机制浅析 [3]

live messenger与稀疏文件—Sparse File Bit

live messenger与稀疏文件—Sparse File Bit

又开学了!

自己制作的Rss阅读器(1)

代码整理

DNN中的Skinning系统

DNN中的Skinning系统

Windows Defender (Beta 2)

more..

其他相关文章

more... 搜索

本文关键字

技术: "JSP" "XML"

产品:

人物:

企业:"SUN"

热门关键字

技术

  数据库  XML  JSP  模式

  测试  Servlet

产品

  Tomcat  Struts

  WebLogic  Eclipse  Ant

企业

  SUN  Apache  IBM  Oracle

  microsoft

热点文章

JSF and AJAX

Java 5.0 多线程编程实践

Hibernate; Easy, Scalable Paging of Large Data

使用AJAX和J2EE创建功能强大的瘦客户端

开源数据库系统

J2EE中使用Spring AOP框架和EJB组件

Get Started with Multithreading in .NET

Exadel宣布AJAX在Studio Pro 3.5中支持JSF

IntelliJ IDEA: Detecting Probable NPEs

IntelliJ IDEA: i18n support

Java J2EE Lite with Spring Framework

比较完整全面的分页

甲骨文曾试图收购MySQL 继续其开源之路

Four Ways to Use Excel as an Internet Reporting Tool

关于权限模块的设想

最新文章

Exadel宣布AJAX在Studio Pro 3.5中支持JSF

Oracle收购BerkeleyDB

BEA宣布开放Kodo源代码—Java持久化API

甲骨文曾试图收购MySQL 继续其开源之路

利用J2EE部署CRM BEA和Siebel共推SOA

Java Web Application开发日志之二--“Ignore how they were created”,工厂方法模式应用

比较完整全面的分页

OSGi Alliance开设面向公众的邮件列表

关于权限模块的设想

oscache使用和研究

开源产品学习方法论

Rife的Continuations

eXtremeComponent在中文环境下的使用

webwork 2.2 中如何给Collection赋值

Get Started with Multithreading in .NET