7

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

4

1 回答 1

9

SyndFeed.getEntries()返回一个ListSyndEntryImpl你不能投 fromSyndEntryImplorg.jdom.Element

SyndEntry您可以按如下方式遍历所有内容:

for (final Iterator iter = feed.getEntries().iterator();
     iter.hasNext(); )
{
    final SyndEntry entry = (SyndEntry) iter.next();
    String title = entry.getTitle();
    String uri = entry.getUri();
    //...
}

API 链接


如果您使用的是 Java 5.0 及更高版本,也可以试试这个:

for (SyndEntry entry : (List<SyndEntry>) feed.getEntries()) {
    String title = entry.getTitle();
    String uri = entry.getUri();
    //...
}

这里有未经检查的演员表,但根据getEntries().

也可以看看

于 2010-05-13T09:57:48.480 回答