Java 中如何实现RSS 2.0

最后努力一把,争取今天上排名,提前一天完成任务。好高兴。

这个同样是给初学的一点东西,请大家不要见笑。

在这里我用 dom4j来写xml,希望ice不要再来找我麻烦了,我这个人喜欢用File啊,呵呵,虽然比较麻烦。

首先我们去下载一个dom4j,然后在导入到 Eclipse 的 lib里。

javapath.JPG


这里下载

目标 在《周博通》里,可以更新等。

完成后截图

rss1.JPG

rss2.JPG

代码非常简单。不用解说了吧,呵呵

package org.dong.rss;

import java.io.File;
import java.io.FileWriter;
import java.util.Date;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

public class RssGen {

    public static int createRssFile(String filename) {

        /** 返回操作结果, 0表失败, 1表成功 */

        int returnValue = 0;

        /** 建立document对象 */

        Document document = DocumentHelper.createDocument();

        /** 建立channel */

        Element channelElement = document.addElement("channel");

        /** 加入一行注释 */
        try {

            channelElement.addComment("myao's rss channel demo");

            // channel
            Element titleElement = channelElement.addElement("title");
            Element desElement = channelElement.addElement("description");
            Element linkElement = channelElement.addElement("link");
            // language
            Element langElement = channelElement.addElement("language");
            Element pubDateElement = langElement.addElement("pubDate");
            Element lastBuildDateElement = langElement
                    .addElement("lastBuildDate");
            Element generatorElement = langElement.addElement("generator");
            // item
   

            /** 为channel设置内容 */
            titleElement.setText("myao's rss title");
            desElement.setText("myao's channel");
            linkElement.setText("http://www.blogjava.net/myao");

            // 为language设置内容
            langElement.setText("zh-cn");
            Date dt = new Date();
            pubDateElement.setText(dt.toString());
            lastBuildDateElement.setText(dt.toString());
            generatorElement.setText("myao");

            for (int fori = 0; fori < 10; fori++) {

                Element itemElement = channelElement.addElement("item");
                Element itemtitle = itemElement.addElement("title");
                Element itemdes = itemElement.addElement("description");
                Element itemlink = itemElement.addElement("link");
                Element itemcreator = itemElement.addElement("creator");
                Element itemauthor = itemElement.addElement("author");
                Element itempubDate = itemElement.addElement("pubDate");

//为单个文章设置内容
                itemauthor.setText("myao");
                itemcreator.setText("myao");
                StringBuffer title = new StringBuffer();
                title.append("article ");
                title.append(fori);
                title.append(new Date());
                itempubDate.setText(dt.toString());
                itemtitle.setText(title.toString());
                itemlink.setText("http://www.blogjava.net/myao");
                itemdes.setText("article des" + title.toString());

            }

        } catch (Exception e) {
            System.out.println(e);
        }



        try {

            /** 将document中的内容写入文件中 */

            XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)));

            writer.write(document);

            writer.close();

            /** 执行成功,需返回1 */

            returnValue = 1;

        } catch (Exception ex) {

            ex.printStackTrace();

        }

        return returnValue;

    }

//测试程序
    public static void main(String[] args) {

        if (createRssFile("c:/rssfile.xml") == 1) {
            System.out.println("gen suc");
        } else {
            System.out.println("gen fail");
        }
    }

}
package com.cnfilm.utils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import com.cnfilm.web.film.Film; import com.sun.syndication.feed.rss.Channel; import com.sun.syndication.feed.rss.Description; import com.sun.syndication.feed.rss.Item; import com.sun.syndication.io.FeedException; import com.sun.syndication.io.WireFeedOutput; /** * 文件名:RssUtils.java 网站RSS生成 * 版本信息:V1.0 * 日期:2013-06-18 * Copyright BDVCD Corporation 2013 * 版权所有 http://www.bdvcd.com */ public class RssUtils { public static String getRssString(List filmList,HashMap map){ Channel channel = new Channel("rss_2.0"); channel.setTitle(map.get("title")); channel.setDescription(map.get("description")); channel.setLink("http://www.bdvcd.com/"); channel.setEncoding("UTF-8"); /**这个list对应rss的item列表**/ List items = new ArrayList(); /**新建Item对象,对应rss的**/ Item item = null; for(Film film:filmList){ item = new Item(); item.setAuthor(film.getStr("starring")); item.setLink("http://www.bdvcd.com/"+film.getStr("curl")+"/"+film.getStr("url")+".html"); item.setPubDate(DateUtils.parse(film.getStr("addtime"))); item.setTitle(film.getStr("fname")); Description description = new Description(); description.setValue(film.getStr("content")); item.setDescription(description); items.add(item); } channel.setItems(items); /**用WireFeedOutput对象输出rss文本**/ WireFeedOutput out = new WireFeedOutput(); String rssString = ""; try { rssString = out.outputString(channel); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (FeedException e) { e.printStackTrace(); } return rssString; } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值