RSSpect - 讀寫RSS2.0的Java類庫

openkk 12年前發布 | 69K 次閱讀 RSS RSS/ATOM相關

特性

  • 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.
  • </ul> </div> </section>

    示例

    • 從本地硬盤的一個XML文件生成一個RSS bean.

      RSS myRSS = new RSSDoc().readRSSToBean(new File("/myPath/myRSS.xml");
      </li>

    • 從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();
      </li>

    • Read an RSS bean into a formatted String.

      String myRssStr = new RSSDoc().readRSSToString(myRSS, "javanet.staxutils.IndentingXMLStreamWriter");
      </li>

    • Write an RSS bean to disk.

      new RSSDoc().writeRSSDoc(new File("/somewhere/myRSS.xml"), myRSS, "UTF-8", "1.0");
      </li>

    • 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");
      </li>

    • 一個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();
          }
      }
      

      } </pre></li> </ul> </div> </section>

      項目主頁:http://www.baiduhome.net/lib/view/home/1337301147010

     本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
     轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
     本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!