【blog】SpringMVC返回RSS格式的XML数据

代码

import com.sun.syndication.feed.rss.Channel;
import com.sun.syndication.feed.rss.Content;
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 java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Author:Mr.X
 * Date:2017/11/12 14:01
 * Description:
 */
public class RSSUtil {

    public static String channleItemXml() {
        Date date = new Date();

        Channel channel = new Channel("rss_2.0"); // 该type参数为固定格式
        channel.setTitle("test rome channel title");
        channel.setDescription("channel的描述");
        channel.setLink("http://hi.baidu.com/openj/rss");
        channel.setPubDate(date);
        channel.setEncoding("GBK");

        List items = new ArrayList();
        Item item = new Item();
        item.setAuthor("zhangwei");
        item.setTitle("item title");

        Description desc = new Description();
        desc.setType("item desc type");
        desc.setValue("item desc value");
        item.setDescription(desc);

        items.add(item);// 添加一个item

        Item item2 = new Item();
        item2.setAuthor("zhangwei");
        item2.setTitle("use rome to read rss");

        Description desc2 = new Description();
        desc2.setValue("you must import rome_1.0.jar & jdom.jar");
        item2.setDescription(desc2);
        Content content = new Content();
        content.setValue("rome是用来发布读取rss的工具,遵循rss标准的XML");
        item2.setContent(content);

        items.add(item2);// 添加一个item

        channel.setItems(items);

        WireFeedOutput out = new WireFeedOutput();
        String str = "";
        try {
            str = out.outputString(channel);
        } catch (FeedException ex) {
            ex.printStackTrace();
        }
        return str;
    }

    public static void main(String[] args) {
        System.out.println(channleItemXml());
    }
}
@GetMapping(value = "feed", produces = "application/xml")
@ResponseBody
public String feed() {
    return RSSUtil.channleItemXml();
}

效果

1211163-20171112144211669-1870527009.png

转载于:https://www.cnblogs.com/bobi1234/p/7821621.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值