5

How do I get the contents as a String using ROME in Java for some feed.

At the moment this is what I got

String feedURL = “...”;
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedUrl));

System.out.println(feed);

for (final Iterator iter = feed.getModules().iterator(); iter.hasNext();){
    System.out.println("\t" + ((Module)iter.next()).getUri());
}

System.out.println("Titles of the " + feed.getEntries().size() + " entries:");
for (final Iterator iter = feed.getEntries().iterator(); iter.hasNext();){
     System.out.println("\t" + ((SyndEntry)iter.next()).getContents());
}

Then the output of this is:

SyndContentImpl.interface=interface com.sun.syndication.feed.synd.SyndContent
SyndContentImpl.type=html
SyndContentImpl.mode=null

    SyndContentImpl.value=          <p><a href="http://www.flickr.com/people/64539367@N07/">MiscDot</a> posted a photo:</p>

<p><a href="http://www.flickr.com/photos/64539367@N07/5954881384/" title="03a"><img src="http://farm7.static.flickr.com/6024/5954881384_5390838321_m.jpg" width="240" height="240" alt="03a" /></a></p>

But what I want is to just get the string of the contents: SyndContentsImpl.value

Larry
  • 11,439
  • 15
  • 61
  • 84

1 Answers1

4

Something like...

for (Iterator<?> entryIter = syndFeed.getEntries().iterator(); entryIter.hasNext();) {
    SyndEntry syndEntry = (SyndEntry) entryIter.next();

    if (syndEntry.getContents() != null) {
        for (Iterator<?> it = syndEntry.getContents().iterator(); it.hasNext();) {
            SyndContent syndContent = it.next();

            if (syndContent != null) {
                String value = syndContent.getValue();
            }
        }
    }
}
pillingworth
  • 3,238
  • 2
  • 24
  • 49
  • What’s the purpose of the second not-null check? Also could anything go wrong if I replaced the inner for loop with: `List content = sydnEntry.getContents(); String value = content.get(0).getValue();` – Larry Jul 19 '11 at 17:07
  • I cut and paste from some code I have - you're right you shouldn't need the second null check but you will need to do an isEmpty() check before the get(0). – pillingworth Jul 20 '11 at 08:20