【问题标题】:How to read the WebMaster field with Rome如何使用 Rome 阅读 WebMaster 字段
【发布时间】:2013-07-10 17:58:29
【问题描述】:

我正在尝试使用 ROME 来解析这样的 RSS 提要:

url = new URL("http://www.rssboard.org/files/sample-rss-2.xml");
XmlReader reader = new XmlReader(url);
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(reader);
System.out.println(feed.getAuthor());

但是,我找不到获取“WebMaster”字段或任何其他自定义字段的方法。

我从here 阅读了罗马的自定义模块,但我不知道如何使用它。我为webMaster字段创建了一个类似的SamplleModuleSampleModuleImplSampleModule Parser,但我不知道如何使用它!

这是我实现的类: 样品模块:

public interface SampleModule extends Module {

        public static final String URI = 
"http://www.rssboard.org/files/sample-rss-2.xml";

    public String getWebMaster();

    public void setWebMaster(String webMaster);

}

SampleModuleImpl:

public class SampleModuleImpl extends ModuleImpl implements SampleModule {

    private static final long serialVersionUID = 1L;
    private String _webMaster;

    protected SampleModuleImpl() {
        super(SampleModule.class, SampleModule.URI);

    }

    @Override
    public void copyFrom(Object obj) {
        SampleModule sm = (SampleModule) obj;
        setWebMaster(sm.getWebMaster());

    }

    @Override
    public Class getInterface() {
        return SampleModule.class;
    }


    @Override
    public String getWebMaster() {
        return _webMaster;
    }

    @Override
    public void setWebMaster(String webMaster) {
        _webMaster = webMaster;

    }

}

和 SampleModuleParser:

public class SampleModuleParser implements ModuleParser {

    private static final Namespace SAMPLE_NS = Namespace.getNamespace("sample",
            SampleModule.URI);

    @Override
    public String getNamespaceUri() {
        return SampleModule.URI;
    }

    @Override
    public Module parse(Element dcRoot) {
        boolean foundSomething = false;
        SampleModule fm = new SampleModuleImpl();

        Element e = dcRoot.getChild("webMaster");
        if (e != null) {
            foundSomething = true;
            fm.setWebMaster(e.getText());
        }

        return (foundSomething) ? fm : null;
    }

}

我还将这些模块添加到 rome.properties。 我只是不知道如何在我的阅读器方法中使用它们。 有什么想法吗?

【问题讨论】:

  • 您在 rome.properties 中的哪个位置添加了它?确保为要解析的 RSS 版本的 ModuleParser 元素设置它。
  • 这不是问题。问题是我应该在我的代码中添加什么以获得“getWebMaster”方法?我的意思是我的第一个代码。

标签: java module rss feed rome


【解决方案1】:

在此处查看有关如何使用 MRSS 模块执行此操作的示例:

http://ideas-and-code.blogspot.com/2009/07/media-rss-plugin-for-rome-howto.html

基本上,您获取一个 SyndEntry 对象并使用模块的命名空间,如果存在,您可以从条目中获取模块对象的实例,因此在您的情况下:

    SampleModule myModule = (SampleModule)e.getModule( SampleModule.URI );

然后你就可以使用它了。我在解析器中使用 groovy 和 rome 并执行以下操作:

def mediaModule = entry.getModule("http://search.yahoo.com/mrss/")
if(mediaModule) {
mediaModule.getMediaGroups().each { group ->
     group.contents.each { content ->
        if(content.type != null && content.type.startsWith("image")) {
        log.info "got an image"
        String imgUrl = content.getReference().toString()
        post.images.add(new MediaContent(type:'image',url:imgUrl))
        }
     }
    }
}

HTH

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2010-12-21
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多