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

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
Gandalf StormCrow
  • 25,788
  • 70
  • 174
  • 263

1 Answers1

9

SyndFeed.getEntries() returns a List of SyndEntryImpl. You can not cast from SyndEntryImpl to org.jdom.Element.

You can iterate through all SyndEntry as follows:

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

API links


You can also try this if you're using Java 5.0 and above:

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

There is unchecked cast here, but it should be safe based on the specification of getEntries().

See also

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • Great its working, but I have some custom tags inside , how do I get these? And I have a and inisde that I have a another custom tag, how do I get these? One more thing, which approach is better(which one you recommend) iterator or generics – Gandalf StormCrow May 13 '10 at 10:47
  • 1
    @gandalf: The choice is not iterator vs generics, but rather explicit iterator or implicit one in for-each. For-each, whenever applicable, is always better. – polygenelubricants May 13 '10 at 10:56
  • 1
    @gandalf: As to how to get the contents, go to the API links I gave above and look around. Try `System.out.println(entry.getContents());` and just experiment. I've never used Rome, all I know about it is what the documentation says. – polygenelubricants May 13 '10 at 11:00