通过 ROME 解析 RSS 提要,获取 null 作为源

发布于 2024-12-16 12:40:44 字数 701 浏览 4 评论 0原文

我正在使用 com.sun.syndicate api 来解析我最终需要在应用程序中使用的 RSS 提要。我面临的问题是,对于某些 RSS,例如,

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

如果我们有带有某个值的标签,则以下代码将返回 null 作为源。

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());
      }

有人知道我在这里可能会错过什么吗

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪意 2024-12-23 12:40:44

这似乎无法开箱即用,因为 RSS 2.0 的转换器忽略(可选)源元素。

您可以编写自己的转换器并根据项目的源属性自行设置源。我将值(您提要中的“AP”)放入此处的作者字段中:

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;
    }
}

幸运的是,通过更改 rome.properties 文件并设置 MyConverterForRSS20<,可以轻松地将自定义转换器插入到 rome 中/code> 而不是 com.sun.synmination.feed.synd.impl.ConverterForRSS20 (文件的最后一行):

# 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 

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文