我正在尝试在Java中构建一个轮询服务,用于检查来自RSS提要的更新。
在检测新项目时,它应该只在系统中进一步发送新项目。
是否有API执行此操作或我是否必须自行进行比较检查?
目前,我的轮询器只返回它当前看到的导致系统重复的内容。
答案 0 :(得分:1)
Sun有一个RSS Utilities库,用于创建供稿。然而,它还包括一个有用的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
}
答案 1 :(得分:0)
答案 2 :(得分:0)
我是java的新手......但这是我尝试过的简单代码,效果很好。我没有从特定网站上阅读RSS,而是从本地目录中读取RSS。使用http://informa.sourceforge.net/
上提供的Informa APIpublic 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");
}
}