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

About this user

ranjan

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

RSS Reader - Reads Name and URL into HashMap

// description of your code here
// RSS reader for web reads them into HashMap

   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  import java.util.HashMap;
  14  
  15  import com.sun.syndication.feed.module.Module;
  16  import com.sun.syndication.feed.synd.SyndEntry;
  17  import com.sun.syndication.feed.synd.SyndFeed;
  18  import com.sun.syndication.io.SyndFeedInput;
  19  import com.sun.syndication.io.XmlReader;
  20  
  21  /**
  22   * Reads and prints any RSS/Atom feed type. Adopted from the example by the
  23   * same name at http://wiki.java.net/bin/view/Javawsxml/Rome05TutorialFeedReader
  24   *
  25   */
  26  public class FeedReader {
  27  
  28  
  29      HashMap hm = null;
  30      String[][] rss = null ;
  31      SyndFeedInput input ;
  32      URL feedUrl;
  33      SyndFeed feed ;
  34      int count =-1;
  35  
  36  
  37      public HashMap readRSS(String url) {
  38          boolean readOk = false;
  39  
  40              try {
  41  
  42                  hm = new HashMap();
  43  
  44                 feedUrl  = new URL(url);
  45  
  46                  input  = new SyndFeedInput();
  47                   feed = input.build(new XmlReader(feedUrl));
  48  
  49                  System.out.println("Title: " + feed.getTitle());
  50                  System.out.println("Author: " + feed.getAuthor());
  51                  System.out.println("Description: " + feed.getDescription());
  52                  System.out.println("Pub date: " + feed.getPublishedDate());
  53                  System.out.println("Copyright: " + feed.getCopyright());
  54                  System.out.println("Modules used:");
  55  
  56  
  57  
  58                  String metaRSS = "Title: " + feed.getTitle() + "\n" +
  59                  "Author: " + feed.getAuthor()  + "\n" +
  60                   "Description: " + feed.getDescription()  + "\n" +
  61                   "Pub date: " + feed.getPublishedDate()  + "\n" +
  62                   "Copyright: " + feed.getCopyright() ;
  63  
  64  
  65  
  66  
  67                  rss = new String[ feed.getEntries().size()][2];
  68  
  69  
  70                  System.out.println("Titles of the " + feed.getEntries().size() +
  71                                     " entries:");
  72                  for (final Iterator iter = feed.getEntries().iterator();
  73                       iter.hasNext();)
  74                  {
  75  
  76  
  77  
  78  
  79  
  80  
  81                      rss[++count][0] =      ((SyndEntry)iter.next()).getTitle().toString();
  82  
  83  
  84  
  85  
  86                  }
  87                  count = -1 ;
  88                  for (final Iterator iter = feed.getEntries().iterator();
  89                       iter.hasNext();)
  90                  {
  91  
  92                     rss[++count][1] =      ((SyndEntry)iter.next()).getUri().toString();
  93                  }
  94  
  95  
  96                  if (feed.getImage() != null)
  97                  {
  98                      System.out.println("Feed image URL: " +
  99                                         feed.getImage().getUrl());
 100                  }
 101  
 102                  readOk = true;
 103                  hm.put( feed.getTitle(), rss);
 104              }
 105              catch (Exception ex) {
 106                  ex.printStackTrace();
 107                  System.out.println("ERROR: " + ex.getMessage());
 108              }
 109  
 110  
 111          String[][] rs = (String[][])hm.get("LinuxInsider");
 112  
 113            System.out.println("************************");
 114          for( int i=0; i<rs.length; i++){
 115  
 116              System.out.println( rs[i][0]);
 117               System.out.println( rs[i][1]);
 118  //             System.out.println( rs[i][2]);
 119          }
 120          if (! readOk) {
 121              System.out.println();
 122              System.out.println("FeedReader reads and prints info on any RSS/Atom feed.");
 123              System.out.println("The first parameter must be the URL of the feed to read.");
 124              System.out.println();
 125          }
 126  
 127          return hm;
 128      }
 129  
 130  }

RSS Reader - simple with Main

// 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  }
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS