Question

I am using com.sun.syndication apis for parsing the RSS feeds which i need to eventually use in my application. The problem that i am facing is that for some of the RSS e.g. like

http://rss.news.yahoo.com/rss/mostviewed

Where we have tag with some value the following code returns null as source.

URL url  = new URL("http://rss.news.yahoo.com/rss/mostviewed");
    XmlReader reader = null;

    try {
      reader = new XmlReader(url);
      SyndFeed feed = new SyndFeedInput().build(reader);
      System.out.println("Feed Title: "+ feed.getTitle());

      for (Iterator<SyndEntry> i =feed.getEntries().iterator(); i.hasNext();) {
          SyndEntry entry = i.next();
              System.out.println("entry.getSource():"+entry.getSource());
      }

Does someone has any idea on what i could just be missing here

Was it helpful?

Solution

This does not seem to work out of the box because the converter for RSS 2.0 is ignoring the (optional) source element.

You could write you own converter and set the source yourself from the item's source attribute. I put the value ("AP" from you feed) into the author field here:

public class MyConverterForRSS20 extends ConverterForRSS20 {

    public MyConverterForRSS20() {
        this("rss_2.0");
    }

    protected MyConverterForRSS20(String type) {
        super(type);
    }

    @Override
    protected SyndEntry createSyndEntry(Item item, boolean preserveWireItem) {
        SyndEntry syndEntry = super.createSyndEntry(item, preserveWireItem);
        Source source = item.getSource();
        if (source != null) {
            SyndFeed syndFeed = new SyndFeedImpl();
            syndFeed.setLink(source.getUrl());
            syndFeed.setAuthor(source.getValue());
            syndEntry.setSource(syndFeed);
        }
        return syndEntry;
    }
}

Fortunately, the custom converter can be easily plugged into rome by changing the rome.properties file and setting MyConverterForRSS20 instead of com.sun.syndication.feed.synd.impl.ConverterForRSS20 (last line of file):

# Feed Conversor implementation classes
#
Converter.classes=com.sun.syndication.feed.synd.impl.ConverterForAtom10 \
                  com.sun.syndication.feed.synd.impl.ConverterForAtom03 \
                  com.sun.syndication.feed.synd.impl.ConverterForRSS090 \
                  com.sun.syndication.feed.synd.impl.ConverterForRSS091Netscape \
                  com.sun.syndication.feed.synd.impl.ConverterForRSS091Userland \
                  com.sun.syndication.feed.synd.impl.ConverterForRSS092 \
                  com.sun.syndication.feed.synd.impl.ConverterForRSS093 \
                  com.sun.syndication.feed.synd.impl.ConverterForRSS094 \
                  com.sun.syndication.feed.synd.impl.ConverterForRSS10  \
                  MyConverterForRSS20 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top