Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

RSS Reader - simple with Main (See related posts)

// description of your code here
//Reads rss from given url

   1  
   2  
   3  
   4  /**
   5   * Created by IntelliJ IDEA.
   6   * User: Rapid
   7   * Date: Oct 9, 2006
   8   * Time: 3:18:23 PM
   9   * To change this template use File | Settings | File Templates.
  10   */
  11  import java.net.URL;
  12  import java.util.Iterator;
  13  
  14  import com.sun.syndication.feed.module.Module;
  15  import com.sun.syndication.feed.synd.SyndEntry;
  16  import com.sun.syndication.feed.synd.SyndFeed;
  17  import com.sun.syndication.io.SyndFeedInput;
  18  import com.sun.syndication.io.XmlReader;
  19  
  20  /**
  21   * Reads and prints any RSS/Atom feed type. Adopted from the example by the
  22   * same name at http://wiki.java.net/bin/view/Javawsxml/Rome05TutorialFeedReader
  23   *
  24   */
  25  public class FeedReader1 {
  26  
  27      public static void main(final String[] args) {
  28          boolean readOk = false;
  29          if (args.length == 1) {
  30              try {
  31                  final URL feedUrl = new URL(args[0]);
  32  
  33                  final SyndFeedInput input = new SyndFeedInput();
  34                  final SyndFeed feed = input.build(new XmlReader(feedUrl));
  35  
  36                  System.out.println("Title: " + feed.getTitle());
  37                  System.out.println("Author: " + feed.getAuthor());
  38                  System.out.println("Description: " + feed.getDescription());
  39                  System.out.println("Pub date: " + feed.getPublishedDate());
  40                  System.out.println("Copyright: " + feed.getCopyright());
  41                  System.out.println("Modules used:");
  42                  for (final Iterator iter = feed.getModules().iterator();
  43                       iter.hasNext();)
  44                  {
  45                      System.out.println("\t" + ((Module)iter.next()).getUri());
  46                  }
  47                  System.out.println("Titles of the " + feed.getEntries().size() +
  48                                     " entries:");
  49                  for (final Iterator iter = feed.getEntries().iterator();
  50                       iter.hasNext();)
  51                  {
  52                      System.out.println("\t" +
  53                                         ((SyndEntry)iter.next()).getTitle());
  54                      
  55                  }
  56                  if (feed.getImage() != null)
  57                  {
  58                      System.out.println("Feed image URL: " +
  59                                         feed.getImage().getUrl());
  60                  }
  61  
  62                  readOk = true;
  63              }
  64              catch (Exception ex) {
  65                  ex.printStackTrace();
  66                  System.out.println("ERROR: " + ex.getMessage());
  67              }
  68          }
  69  
  70          if (! readOk) {
  71              System.out.println();
  72              System.out.println("FeedReader reads and prints info on any RSS/Atom feed.");
  73              System.out.println("The first parameter must be the URL of the feed to read.");
  74              System.out.println();
  75          }
  76      }
  77  }

You need to create an account or log in to post comments to this site.


Click here to browse all 5349 code snippets

Related Posts