使用 Rome 循环浏览 Feed 条目
我试图循环遍历 Atom feed 条目,并获取 title 属性,比如说,我发现 这篇 文章,我尝试了这段代码片段:
for (final Iterator iter = feeds.getEntries.iterator();
iter.hasNext(); )
{
element = (Element)iter.next();
key = element.getAttributeValue("href");
if ((key != null) &&
(key.length() > 0))
{
marks.put(key, key);
}
//Don't have to put anything into map just syso title would be enough
}
但我收到异常消息:
java.lang.ClassCastException: com.sun.syndicate.feed.synd.SyndEntryImpl 无法转换为 org.jdom.Element at com.emir.altantbh.FeedReader.main(FeedReader.java:47)
我做错了什么?任何人都可以指导我更好的教程或告诉我我在哪里犯了错误,我需要循环条目并提取标题标签值。谢谢
I'm trying to loop through Atom feed entries, and get the title attribute lets say, I found this article, I tried this snipped of code :
for (final Iterator iter = feeds.getEntries.iterator();
iter.hasNext(); )
{
element = (Element)iter.next();
key = element.getAttributeValue("href");
if ((key != null) &&
(key.length() > 0))
{
marks.put(key, key);
}
//Don't have to put anything into map just syso title would be enough
}
But I get exception saying :
java.lang.ClassCastException:
com.sun.syndication.feed.synd.SyndEntryImpl
cannot be cast to org.jdom.Element at
com.emir.altantbh.FeedReader.main(FeedReader.java:47)
What did I do wrong? can anyone direct me towards better tutorial or show me where did I make mistake, I need to loop through entries and extract title tag value. thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SyndFeed.getEntries()
返回SyndEntryImpl
的List
。您无法从SyndEntryImpl
转换为org.jdom.Element
。您可以按如下所示迭代所有
SyndEntry
:API 链接
com.sun.synmination.feed.synd.SyndEntry
com.sun.synmination.feed.synd.SyndFeed
java.util.List
您还可以如果您使用的是 Java 5.0 及更高版本,请尝试此操作:
此处存在未经检查的强制转换,但根据
getEntries()
的规范,它应该是安全的。另请参阅
SyndFeed.getEntries()
returns aList
ofSyndEntryImpl
. You can not cast fromSyndEntryImpl
toorg.jdom.Element
.You can iterate through all
SyndEntry
as follows:API links
com.sun.syndication.feed.synd.SyndEntry
com.sun.syndication.feed.synd.SyndFeed
java.util.List
You can also try this if you're using Java 5.0 and above:
There is unchecked cast here, but it should be safe based on the specification of
getEntries()
.See also