RSS实现生成xml

所需jar包

com.sun.syndication.jar

代码:

package utils;

import com.sun.syndication.feed.rss.Category;
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;
import com.weixin.model.news.News;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * RSS生成XML
 * create by frank
 * on 2018/02/01
 */
public class RSSXmlUtils {

    public static String xmlBuilder(List<News> modelList) {

        Channel channel = new Channel("rss_2.0");
        channel.setTitle("channel标题");//网站标题
        channel.setDescription("channel的描述");//网站描述
        channel.setLink("www.shlll.net");//网站主页链接
        channel.setEncoding("utf-8");//RSS文件编码
        channel.setLanguage("zh-cn");//RSS使用的语言
        channel.setTtl(5);//time to live的简写,在刷新前当前RSS在缓存中可以保存多长时间(分钟)
        channel.setCopyright("版权声明");//版权声明
        List<Item> items = new ArrayList<Item>();//这个list对应rss中的item列表

        for (News news : modelList) {
            Item item = new Item();//新建Item对象,对应rss中的<item></item>
            item.setAuthor(news.getAuthor());//对应<item>中的<author></author>
            item.setTitle(news.getTitle());//对应<item>中的<title></title>
            item.setPubDate(news.getPublishTime());//这个<item>对应的发布时间
            item.setComments(news.getComments());//代表<item>节点中的<comments></comments>
            //新建一个Description,它是Item的描述部分
            Description description = new Description();
            description.setValue(news.getCotent());//<description>中的内容
            item.setDescription(description);

            //新建Category 添加种类
            List cates = new ArrayList();
            Category category = new Category();
            category.setValue(news.getCategory());
            cates.add(category);
            item.setCategories(cates);


            items.add(item);//代表一个段落<item></item>,
        }

        channel.setItems(items);
        WireFeedOutput out = new WireFeedOutput();
        try {
            String xml = out.outputString(channel);
            return xml;
        } catch (FeedException e) {
            e.printStackTrace();
            return null;
        }


    }

    public static void main(String args[]) {

        List<News> newsList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            News news = new News();
            news.setAuthor("frank" + i);
            news.setComments("comments" + i);
            news.setCotent("comtent" + i);
            news.setTitle("title" + i);
            news.setPublishTime(new Date());

            news.setCategory("cate1");
            newsList.add(news);
        }

        String xml = RSSXmlUtils.xmlBuilder(newsList);

        System.out.println(xml);
    }
}

运行图:

这里写图片描述

解析代码:

 public void parseRss() {

        String rss = "http://rss.sina.com.cn/ent/hot_roll.xml";
        try {
            URL url = new URL(rss);
            // 读取Rss源     
            XmlReader reader = new XmlReader(url);
            System.out.println("Rss源的编码格式为:" + reader.getEncoding());
            SyndFeedInput input = new SyndFeedInput();
            // 得到SyndFeed对象,即得到Rss源里的所有信息     
            SyndFeed feed = input.build(reader);
            //System.out.println(feed);           
            // 得到Rss新闻中子项列表     
            List entries = feed.getEntries();
            // 循环得到每个子项信息     
            for (int i = 0; i < entries.size(); i++) {
                SyndEntry entry = (SyndEntry) entries.get(i);
                // 标题、连接地址、标题简介、时间是一个Rss源项最基本的组成部分     
                System.out.println("标题:" + entry.getTitle());
                System.out.println("连接地址:" + entry.getLink());
                SyndContent description = entry.getDescription();
                System.out.println("标题简介:" + description.getValue());
                System.out.println("发布时间:" + entry.getPublishedDate());
                // 以下是Rss源可先的几个部分     
                System.out.println("标题的作者:" + entry.getAuthor());
                // 此标题所属的范畴     
                List categoryList = entry.getCategories();
                if (categoryList != null) {
                    for (int m = 0; m < categoryList.size(); m++) {
                        SyndCategory category = (SyndCategory) categoryList.get(m);
                        System.out.println("此标题所属的范畴:" + category.getName());
                    }
                }
                // 得到流媒体播放文件的信息列表     
                List enclosureList = entry.getEnclosures();
                if (enclosureList != null) {
                    for (int n = 0; n < enclosureList.size(); n++) {
                        SyndEnclosure enclosure = (SyndEnclosure) enclosureList.get(n);
                        System.out.println("流媒体播放文件:" + entry.getEnclosures());
                    }
                }
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值