Java – loop through feed entries in Rome
I'm trying to loop through the atom feed entries and get the title attribute. Let's say, I found this article, and I tried this clip 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 the exception:
What did I do wrong? Anyone can guide me to a better tutorial or tell me where I made a mistake. I need to cycle through the entries and extract the title tag value thank you
Solution
SyndFeed. Getentries() returns the list of syndentryimpl You cannot cast from syndentryimpl to org jdom. Element.
You can traverse all syndentries 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 link
> com. sun. syndication. feed. synd. SyndEntry > com. sun. syndication. feed. synd. SyndFeed > java. util. List
If you are using Java 5.0 and later, you can also try this:
for (SyndEntry entry : (List<SyndEntry>) Feed.getEntries()) { String title = entry.getTitle(); String uri = entry.getUri(); //... }
There is an unchecked cast, but it should be based on the getentries () specification
You can also have a look
> Java language guide/for-each loop > Java tutorials/generics