RSSpect - 读写RSS2.0的Java类库

openkk 13年前

特性

  • RSSpect能够很方便读写RSS新闻源
  • 拥有直观,开发友好的API
  • 在类库中的对象都是序列化,不可改变的和线程安全。
  • Fully tested for accuracy (See the distributed test-sources.jar for reference).
  • Minimal dependencies. RSSpect only requires a stax-api implementation to work.
  • In the spirit of Open Source Software, RSSpect is licensed with the terms of the Apache License, Version 2.0.

示例

  • 从本地硬盘的一个XML文件生成一个RSS bean.
    RSS myRSS = new RSSDoc().readRSSToBean(new File("/myPath/myRSS.xml");
  • 从Web的一个XML文件生成一个RSS bean
    RSS myRSS = new RSSDoc().readRSSToBean(new URL("http://www.abcdefg.net/myRSS.xml");
  • Read an RSS bean into a String.
    String myRssStr = myRSS.toString();
  • Read an RSS bean into a formatted String.
    String myRssStr = new RSSDoc().readRSSToString(myRSS, "javanet.staxutils.IndentingXMLStreamWriter");
  • Write an RSS bean to disk.
    new RSSDoc().writeRSSDoc(new File("/somewhere/myRSS.xml"), myRSS, "UTF-8", "1.0");
  • Write a formatted RSS bean to disk.
    new RSSDoc().writeRSSDoc(new javanet.staxutils.IndentingXMLStreamWriter( XMLOutputFactory.newInstance().createXMLStreamWriter( new FileOutputStream("/somewhere/myRSS.xml"), "UTF-8")), myRSS, "UTF-8", "1.0");
  • 一个CRUD(增/删/改/查)示例:
    public class CrudExample {     public static void main(String[] args) {    try {     RSSDoc rssDoc = new RSSDoc();       // make sure title or description is present for item     Title title = rssDoc.buildTitle("my title");     Description description = rssDoc.buildDescription("my description");     Item item = rssDoc.buildItem(title, null, description, null, null,       null, null, null, null, null, null);       // title, link and description are required for channel     // we add pubDate and items because these are usually there     Link link = rssDoc       .buildLink("http://www.colorfulsoftware.com/projects/rsspect");     List items = new ArrayList();     PubDate pubDate = rssDoc.buildPubDate(Calendar.getInstance()       .getTime().toString());     items.add(item);     Channel channel = rssDoc.buildChannel(title, link, description,       null, null, null, null, pubDate,       null, null, null, null, null, null,       null, null, null, null, null, null,       items);          //rss element require channel and version attribue     Attribute rssVersion = rssDoc.buildAttribute("version","2.0");     List attributes = new ArrayList();     attributes.add(rssVersion);     RSS rss = rssDoc.buildRSS(channel, attributes, null);          System.out.println("rss 1 : "+rss);          rssDoc.writeRSSDoc(new File("out1.xml"), rss, rssDoc.getEncoding(),rssDoc.getXmlVersion());          RSS newRSS = rssDoc.readRSSToBean(new File("out1.xml"));          System.out.println("newRSS 1 : "+newRSS);          //should print false     System.out.println("1) rss equal newRSS? "+rss.equals(newRSS));          //add the missing generator to rss     channel = rssDoc.buildChannel(channel.getTitle(), channel.getLink(), channel.getDescription(),       null, null, null, null, channel.getPubDate(),       null, null, rssDoc.getLibVersion(), null, null, null,       null, null, null, null, null, null,       items);     rss = rssDoc.buildRSS(channel, attributes, null);          System.out.println("rss 2 : "+rss);          //should print true     System.out.println("2) rss equal newRSS? "+rss.equals(newRSS));          //make an update     Attribute xmlns = rssDoc.buildAttribute("xmlns:html","http://www.w3.org/1999/xhtml");     attributes = new ArrayList();     attributes.add(rssVersion);     attributes.add(xmlns);     Extension extension = rssDoc.buildExtension("html:html", attributes,"web markupHello Web.");     List extensions = new ArrayList();     extensions.add(extension);     newRSS = rssDoc.buildRSS(channel, attributes, extensions);           System.out.println("newRSS 2 : "+newRSS);          //should print false     System.out.println("3) rss equal newRSS? "+rss.equals(newRSS));               //should still be able to write to file     rssDoc.writeRSSDoc(new File("out2.xml"), newRSS, rssDoc.getEncoding(),rssDoc.getXmlVersion());          //delete the extension     attributes = new ArrayList();     attributes.add(rssVersion);     newRSS = rssDoc.buildRSS(channel, attributes, null);          System.out.println("newRSS 3 : "+newRSS);          //should print true     System.out.println("4) rss equal newRSS? "+rss.equals(newRSS));               //another way to write the feed to file     BufferedWriter out = new BufferedWriter(new FileWriter("out3.xml"));     out.write(newRSS.toString());     out.close();          //another way to consume xml into a bean     newRSS = rssDoc.readRSSToBean(rss.toString());          System.out.println("newRSS 4 : "+newRSS);          //should print true     System.out.println("5) rss equal newRSS? "+rss.equals(newRSS));          System.out.println("finished.");         } catch (Exception e) {     System.out.println("error with feed: " + e.getMessage());     e.printStackTrace();    }   }  }   

项目主页:http://www.open-open.com/lib/view/home/1337301147010