2010-02-18 3 views
3

RSS 피드의 업데이트를 확인하는 폴링 서비스를 Java로 작성하려고합니다.RSS 항목을 Java로 집계하기

새 항목을 검색하면 시스템에서 새 항목 만 추가로 보내야합니다.

이 작업을 수행하는 API가 있습니까? 아니면 직접 비교 검사를해야합니까?

현재 내 폴러는 현재 내 시스템에서 중복을 일으키는 것으로 보이는 내용을 반환합니다. 필요한 것과 유사

+0

API에서 제공 인포 API를 사용하여? 'obj1.equals (obj2)'와 같은가? –

답변

1

일하던 RSS 유틸리티 라이브러리가 "폴러 모듈은 채널 변경의 배경 폴링 편리한 서비스를 제공하기위한 것입니다" 피드를 만들기 위해 만들어졌습니다. 그러나 그것은 또한 비슷한 일을하기 위해 사용하고있는 유용한 RSS 파서를 포함합니다.

http://java.sun.com/developer/technicalArticles/javaserverpages/rss_utilities/

는 새 항목을 확인 단지 GUID를 얻을에 대한 GUID를과 비교하려면 :

당신은 여기에서 라이브러리를 다운로드 할 수 있습니다

은 (파서에 대한 자세한 내용은 아래로 스크롤) 기존 항목.

// Create an RSS Parser 
RssParser parser = RssParserFactory.createDefault(); 

// Parse the feed 
Rss rss = parser.parse(new URL(YOUR_FEED)); 

// Get the channel 
Channel channel = rss.getChannel(); 

// Get the items 
Collection<Item> items = channel.getItems(); 

// Loop for each item 
for (Item item : items) 
{ 
    // Get the GUID 
    Guid guid = item.getGuid(); 

    // Loop for each of the previously seen GUIDs and compare 
} 
+0

이것은 실제 다운로드 링크입니다 : http://java.sun.com/developer/technicalArticles/javaserverpages/rss_utilities/rss_utils_1.1.zip –

0

그럼 내가 자바에 새로운 오전 ...하지만 여기에 내가 시도하고 잘 작동 간단한 코드입니다. 특정 웹 사이트에서 RSS를 읽는 대신 로컬 디렉토리에서 RSS를 읽습니다. 비교를 위해 http://informa.sourceforge.net/

public class Read_UpdateRSS implements de.nava.informa.utils.poller.PollerObserverIF { 

public static void main(String[] args) { 

try { 

     File in = new File("/home/RSSFeed/rssfeed.xml"); 

     ChannelBuilder build = new ChannelBuilder(); 

     Channel channel = (Channel) FeedParser.parse(build,in); 
     System.out.println("Description:" + channel.getDescription()); 
     System.out.println("Title:" + channel.getTitle()); 

     // Magic of polling starts here. polling is done every 10 minutes 

     Poller poll = new Poller(); 
     PollerObserverIF observer = new Read_UpdateRSS(); 
     poll.addObserver(observer); 
     poll.registerChannel(channel, 10 * 60 * 1000); 

     for(Object x: channel.getItems()){ 

      Item anItem = (Item) x; 
      System.out.println(anItem.getTitle() + "-"); 
      System.out.println(anItem.getDescription()); 
    } 

    } catch (Exception e) { 

    } 
} 

@Override 
public void channelChanged(ChannelIF arg0) {} 

@Override 
public void channelErrored(ChannelIF arg0, Exception arg1) {} 

@Override 
public void itemFound(ItemIF item, ChannelIF channel) { 

    System.out.println("new item found"); 
    channel.addItem(item); 
} 

@Override 
public void pollFinished(ChannelIF channel) { 
    System.out.println("Finished polling with " + channel.getItems().size() + " items in the channel"); 

} 

@Override 
public void pollStarted(ChannelIF channel) { 
    System.out.println("Started polling with " + channel.getItems().size() + " items in the channel"); 

} 

}

관련 문제