Monday, June 6, 2011

Reading rss and save to file v.2

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rssreader;

/**
 *
 * @author Zidny
 */
import com.sun.syndication.feed.synd.*;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.net.MalformedURLException;

import java.net.URL;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Iterator;

public class ParseRSSFeedUsingRome {

    public static void readRSS(String feed) throws Exception{
        String dir = "C:\\korpusberita\\";
        URL feedUrl = new URL(feed);

        SyndFeedInput input = new SyndFeedInput();
        SyndFeed sf = input.build(new XmlReader(feedUrl));

        List entries = sf.getEntries();
        Iterator it = entries.iterator();
        while (it.hasNext()) {
            SyndEntry entry = (SyndEntry)it.next();
            System.out.println(entry.getTitle());
            System.out.println(entry.getPublishedDate());
            System.out.println(entry.getLink());
            SyndContent description = entry.getDescription();
            System.out.println(description.getValue());
            System.out.println();

            File f;
            String fileName = dir+entry.getTitle()+".txt";
            try{
                // Create file
                FileWriter fstream = new FileWriter(fileName);
                BufferedWriter out = new BufferedWriter(fstream);
                SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");
                StringBuilder string = new StringBuilder(dateFormat.format(entry.getPublishedDate()));
                out.write("Title: "+entry.getTitle()+"\n");
                out.write("; Link: "+entry.getLink()+"\n");
                out.write("; Date: "+string+"\n");
                out.write("; Description: "+description.getValue()+"\n");
                //Close the output stream
                out.close();
            }catch (Exception e){//Catch exception if any
                System.err.println("Error: " + e.getMessage());
            }
        }
    }

    public static void main(String args[]) throws Exception {
        //String feed = "http://www.voanews.com/templates/Articles.rss?sectionPath=/indonesian/news";
        String[] feed = {"http://www.bbc.co.uk/indonesia/index.xml",
                         "http://www.voanews.com/templates/Articles.rss?sectionPath=/indonesian/news",
                         "http://detik.feedsportal.com/c/33613/f/589693/index.rss",
                         "http://republika.co.id/rss/koran",
                         "http://www.antara.co.id/rss/news.xml",
                         "http://news.okezone.com/rss",
                         "http://www.liputan6.com/feed/rss2/"};

        for(int i=0;i<feed.length;i++){
            readRSS(feed[i]);
        }
      
      
    }//end of main
}

No comments:

Post a Comment