• RSS文章订阅及生成RSS格式的xml


    现在很多的新闻网站,博客网站等都提供了RSS订阅功能,我们可以很方便的进行RSS订阅,下面我使用rome-0.9.jar进行RSS订阅,具体代码如下:

    如订阅网页新闻,先要得到该RSS订阅的地址(xml页面):http://news.163.com/special/00011K6L/rss_gn.xml

    URL feedUrl = new URL("http://news.163.com/special/00011K6L/rss_gn.xml");
               SyndFeedInput input = new SyndFeedInput();
            SyndFeed feed = input.build(new XmlReader(feedUrl));
            System.out.println(feed);
            
            //从feed中得到entry
            List list = feed.getEntries();
            for (int i=0; i< list.size(); i++) {
                SyndEntry entry = (SyndEntry)list.get(i);
                System.out.println("链接为 = "+entry.getLink());
                System.out.println("标题为 = "+entry.getTitle());
                System.out.println("时间为 = "+entry.getPublishedDate());
                System.out.println("内容为 = "+entry.getDescription().getValue());
                System.out.println("Categories为 = "+entry.getCategories().get(0));
                System.out.println("==============================================================================");
            
            } 

    非常方便,同样使用该包来创建RSS格式的xml进行订阅操作也是非常方便的,下面进行示例:

    package djn.test.rss.rome;
    
    import com.sun.syndication.feed.rss.*;
    import com.sun.syndication.io.FeedException;
    import com.sun.syndication.io.WireFeedOutput;
    import java.util.*;
    
    public class RssFeedFactory {
       
        public static void main(String [] args){
            //新建Channel对象,对应rss中的<channel></channel>
            
            /* Channel对象有两个构造器,一个默认的无参构造器用于clone对象,
            * 平时创建Channel对象时只能使用有参构造器Channel(String type)
            * 这个参数type很讲究,起初我随便填写了一些字符串,都抛出异常,非法的type
            * 后来逼急了,上网把rome源码搞下来,才搞明白type得是"rss_x.x"这样的
            * rome的文档里也没有写明,浪费了不少时间研究这个type究竟应该是什么。
            */
            Channel channel = new Channel("rss_2.0");
            channel.setTitle("Test RSS channel's title");
            channel.setDescription("channel的描述");
            channel.setLink("http://hi.baidu.com/openj/rss");
            channel.setEncoding("GBK");
            
            //这个list对应rss中的item列表
            List items = new ArrayList();
            //新建Item对象,对应rss中的<item></item>
            Item item = new Item();
            item.setAuthor("item author jnduan");
            item.setTitle("item title");
            
            //新建一个Description,它是Item的描述部分
            Description description = new Description();
            description.setType("item description type");
            description.setValue("item description value");
            item.setDescription(description);
            
            items.add(item);
            channel.setItems(items);
            
            //用WireFeedOutput对象输出rss文本
            WireFeedOutput out = new WireFeedOutput();
            try {
                System.out.println(out.outputString(channel));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (FeedException e) {
                e.printStackTrace();
            }
        }
    }

    生成的RSS格式的xml
    <?xml version="1.0" encoding="GBK"?>
    <rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
    <channel>
        <title>Test RSS channel's title</title>
        <link>http://hi.baidu.com/openj/rss</link>
        <description>channel的描述</description>
        <item>
          <title>item title</title>
          <description>item description value</description>
          <author>item author jnduan</author>
        </item>
    </channel>
    </rss>

  • 相关阅读:
    Git本地仓库push至GitHub远程仓库每次输入账户密码问题解决(亲测可行)
    Laravel5.5+ 区分前后端用户登录
    word 中Sentences、Paragraph等含义和用法
    Word转图片word
    Word文档编号工具,Word标题,图、表手动编号工具
    Word电子扫描器 Word文档转换为图片Pdf,Word文档扫描成Pdf工具
    如何用vba给一个word表格的最后插入一行
    PPT电子扫描仪 ppt转换为图片Pdf工具
    Word文档只读加密工具
    在c#应用程序中使用IrisSkin2.dll美化界面
  • 原文地址:https://www.cnblogs.com/shenliang123/p/2777103.html
Copyright © 2020-2023  润新知