DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

RSS Reader - Reads Name And URL Into HashMap

10.30.2006
| 5790 views |
  • submit to reddit
        // description of your code here
// RSS reader for web reads them into HashMap



/**
 * Created by IntelliJ IDEA.
 * User: Rapid
 * Date: Oct 9, 2006
 * Time: 3:18:23 PM
 * To change this template use File | Settings | File Templates.
 */
import java.net.URL;
import java.util.Iterator;
import java.util.HashMap;

import com.sun.syndication.feed.module.Module;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

/**
 * Reads and prints any RSS/Atom feed type. Adopted from the example by the
 * same name at http://wiki.java.net/bin/view/Javawsxml/Rome05TutorialFeedReader
 *
 */
public class FeedReader {


    HashMap hm = null;
    String[][] rss = null ;
    SyndFeedInput input ;
    URL feedUrl;
    SyndFeed feed ;
    int count =-1;


    public HashMap readRSS(String url) {
        boolean readOk = false;

            try {

                hm = new HashMap();

               feedUrl  = new URL(url);

                input  = new SyndFeedInput();
                 feed = input.build(new XmlReader(feedUrl));

                System.out.println("Title: " + feed.getTitle());
                System.out.println("Author: " + feed.getAuthor());
                System.out.println("Description: " + feed.getDescription());
                System.out.println("Pub date: " + feed.getPublishedDate());
                System.out.println("Copyright: " + feed.getCopyright());
                System.out.println("Modules used:");



                String metaRSS = "Title: " + feed.getTitle() + "\n" +
                "Author: " + feed.getAuthor()  + "\n" +
                 "Description: " + feed.getDescription()  + "\n" +
                 "Pub date: " + feed.getPublishedDate()  + "\n" +
                 "Copyright: " + feed.getCopyright() ;




                rss = new String[ feed.getEntries().size()][2];


                System.out.println("Titles of the " + feed.getEntries().size() +
                                   " entries:");
                for (final Iterator iter = feed.getEntries().iterator();
                     iter.hasNext();)
                {






                    rss[++count][0] =      ((SyndEntry)iter.next()).getTitle().toString();




                }
                count = -1 ;
                for (final Iterator iter = feed.getEntries().iterator();
                     iter.hasNext();)
                {

                   rss[++count][1] =      ((SyndEntry)iter.next()).getUri().toString();
                }


                if (feed.getImage() != null)
                {
                    System.out.println("Feed image URL: " +
                                       feed.getImage().getUrl());
                }

                readOk = true;
                hm.put( feed.getTitle(), rss);
            }
            catch (Exception ex) {
                ex.printStackTrace();
                System.out.println("ERROR: " + ex.getMessage());
            }


        String[][] rs = (String[][])hm.get("LinuxInsider");

          System.out.println("************************");
        for( int i=0; i<rs.length; i++){

            System.out.println( rs[i][0]);
             System.out.println( rs[i][1]);
//             System.out.println( rs[i][2]);
        }
        if (! readOk) {
            System.out.println();
            System.out.println("FeedReader reads and prints info on any RSS/Atom feed.");
            System.out.println("The first parameter must be the URL of the feed to read.");
            System.out.println();
        }

        return hm;
    }

}