使用Rome 生成RSS

Rome是dev.java.net下的一个开源的项目,是一个“解析、创建、发布RSS和ATOM格式”的工具集,支持RSS 0.90, RSS 0.91 Netscape, RSS 0.91 Userland, RSS 0.92, RSS 0.93, RSS 0.94, RSS 1.0, RSS 2.0, Atom 0.3, and Atom 1.0 等众多版本,对rss和atom中的各个模块都进行了很好的封装,功能很强大。

用Rome生成Rss非常简单,只要你花点时间看看Rome的类库,再花点时间去看看Rome自带的参考例子就差不多了

首先需要把rome.jar和jdom.jar加入classpath.

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.io.SyndFeedOutput;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class RssWriter {

 public boolean ok = false;
 //public String feedType = "rss_2.0";
 private static final String DATE_FORMAT = "yyyy-MM-dd";
 DateFormat dateParser = new SimpleDateFormat(DATE_FORMAT);
 
 private SyndFeed feed = new SyndFeedImpl();

 //这五个一定要齐全,否则会出现错误
 public RssWriter(String host,String title,String link,String feedType,String des){
  feed.setFeedType(feedType);
        feed.setTitle(title);
        feed.setLink(link);
        feed.setAuthor(host);
        feed.setDescription(des);
 }
 
 public RssWriter(String host,String title,String des,String link,String feedType,String Encoding){
        feed.setFeedType(feedType);
        feed.setTitle(title);
        feed.setLink(link);
        feed.setDescription(des);
        feed.setAuthor(host);
        feed.setEncoding(Encoding);
 }
 
 /**
  * 先用map把相对应的bean装载起来放到炼表中,再通过循环取得列表放到feed里面
  */
 public void WriteFeed(String fileName,List arr){
   try{
  List entries = new ArrayList();
        SyndEntry entry;
        SyndContent description;
     for(int i=0;i<arr.size();i++){   
        entry = new SyndEntryImpl();
        description = new SyndContentImpl();
        HashMap hm = (HashMap)arr.get(i);

        entry.setTitle((String) hm.get("title"));
        entry.setAuthor((String) hm.get("author"));
        entry.setUpdatedDate(new Date());
        entry.setLink((String) hm.get("link"));
        entry.setPublishedDate((Date) hm.get("pubdate"));
        description.setType((String) hm.get("type"));
        description.setValue((String) hm.get("content"));
        entry.setDescription(description);
        
        entries.add(entry);
     }
        feed.setEntries(entries);
        Writer writer = new FileWriter(fileName);
        SyndFeedOutput output = new SyndFeedOutput();
        output.output(feed,writer);
        writer.close();
        ok = true;
    }
    catch (Exception ex) {
        ex.printStackTrace();
        System.out.println("ERROR: "+ex.getMessage());
    }
if (!ok) {
    System.out.println();
     System.out.println("---------create the rss file fail.----------");
    System.out.println();
   }
  }
  
}

调用示例:

RssWriter rw = new RssWriter("wzw","wo zai zhe li","hao123.com","rss_2.0","zmhsya");
  
  List list = a.getList();
  List arr = new LinkedList();
  Map hashmap = null;
  for(int i=0;i<list.size();i++){
     Article article = (Article)list.get(i);
     hashmap = new HashMap();
     hashmap.put("title",article.getTitle());
     hashmap.put("author",article.getUserAccountByUserId().getUserName());
     hashmap.put("link",http://www.youziblog.com);
     hashmap.put("pubdate",new Date());
     hashmap.put("type","text/html");
     hashmap.put("content",article.getContent());
     arr.add(hashmap);
  }
  rw.WriteFeed(request.getRealPath("/") + "/rss/articles.rss",arr);
这样在webroot/rss目录下就生成articles.rss了.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot提供了许多属性来配置应用程序,包括抓取RSS的功能。您可以在application.properties或application.yml文件中指定这些属性,也可以通过命令行开关来配置。 要抓取RSS,您可以使用Spring Boot提供的相关类和注解。首先,确保您的项目中已经添加了相关的依赖。在Gradle中,您可以在build.gradle文件中添加以下配置: ```groovy dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'rome:rome' } ``` 然后,您可以创建一个Controller类来处理抓取RSS的请求。使用`@RestController`注解标记该类,并使用`@GetMapping`注解指定处理GET请求的方法。在方法中,您可以使用Rome库来抓取和解析RSS。 ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.rometools.rome.feed.synd.SyndFeed; import com.rometools.rome.io.SyndFeedInput; import com.rometools.rome.io.XmlReader; @RestController public class RssController { @GetMapping("/rss") public SyndFeed getRssFeed() { try { String rssUrl = "https://example.com/rss-feed"; // 替换为实际的RSS源URL SyndFeedInput input = new SyndFeedInput(); SyndFeed feed = input.build(new XmlReader(new URL(rssUrl))); return feed; } catch (Exception e) { // 处理异常 return null; } } } ``` 在上面的示例中,我们创建了一个`RssController`类,并在`/rss`路径上定义了一个GET请求的处理方法。该方法使用Rome库来抓取和解析指定的RSS源,并返回`SyndFeed`对象。 请注意,上述示例仅提供了一个基本的框架,您可能需要根据实际需求进行进一步的处理和定制。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值