RSS Reader - Reads Name and URL into HashMap
// 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 }