簡體   English   中英

如何在羅馬閱讀WebMaster字段

[英]How to read the WebMaster field with Rome

我正在嘗試使用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”字段或任何其他自定義字段的方法。

我從這里已經了解了羅馬的自定義模塊,但是我不知道如何使用它。 我為webMaster字段創建了一個類似的SamplleModuleSampleModuleImplSampleModule Parser,但是我不知道如何使用它!

這是我實現的類:SamplleModule:

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中。 我只是不知道如何在我的閱讀器方法中使用它們。 大家有想法嗎?

在此處查看如何使用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))
        }
     }
    }
}

高溫超導

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM