ROME Library를 이용한 Atom/RSS 읽어오기..

ROME Library는..

ROME is an set of open source Java tools for parsing, generating and publishing RSS and Atom feeds.

The core ROME library depends only on the JDOM XML parser and supports parsing, generating
and converting all of the popular RSS and Atom formats including RSS 0.90, RSS 0.91 Netscape,
RSS 0.91 Userland, RSS 0.92, RSS 0.93, RSS 0.94, RSS 1.0, RSS 2.0, Atom 0.3, and Atom 1.0
.
You can parse to an RSS object model, an Atom object model or an abstract SyndFeed model
that can model either family of formats.

아래는 ROME Library를 이요한 예제 코드입니다.

import java.net.URL;
import java.util.Date;
import java.util.List;

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;

public class RssAtomTest {
    private static String url =”http://www.sjava.net/rss”;
    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
       
        URL feedUrl = new URL(url);
        SyndFeedInput input = new SyndFeedInput();
        SyndFeed syndFeed = input.build(new XmlReader(feedUrl));
               
        System.out.println(“———————– title ——————————“);
        System.out.println(syndFeed.getTitle());
        System.out.println(“————————————————————-“);
        System.out.println(“—————–  updatedAt ————————-“);
        System.out.println(syndFeed.getPublishedDate() );
        System.out.println(“————————————————————-“);
       
        @SuppressWarnings(“unchecked”)
        List<SyndEntry> entries = syndFeed.getEntries();       
        SyndEntry entry = null;
           
        for(int i = 0; i < entries.size(); i++) {
            entry = entries.get(i);
           
            String title = entry.getTitle();
            String url = entry.getUri();
            String content = entry.getDescription().getValue();
            Date updatedAt = entry.getUpdatedDate();
            if(updatedAt == null)
                updatedAt = entry.getPublishedDate();
                           
            System.out.println(“———————————————–“+i+”—————————————————————–“);
            System.out.println(“title : ” + title);
            System.out.println(“——————————————————————————————————————“);
            System.out.println(“url : ” + url);
            System.out.println(“——————————————————————————————————————“);
            System.out.println(“data : “+ content);
            System.out.println(“——————————————————————————————————————“);
            System.out.println(“date : ” + updatedAt.getTime());
            System.out.println(“——————————————————————————————————————“);
        }       
    }

}

답글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.