0

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」フィールドまたはその他のカスタマイズされたフィールドを取得する方法が見つかりません。

ここからローマのカスタムモジュールについて読みましたが、使い方がわかりませんでした。同様SamplleModuleの 、SampleModuleImpl、およびSampleModulewebMaster フィールドのパーサーを作成しましたが、使用方法がわかりません。

これは私が実装したクラスです: 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);

}

サンプルモジュールの実装:

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 に追加しました。リーダーメソッドでそれらを使用する方法がわかりません。アイデアはありますか?

4

1 に答える 1

0

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を使用し、次のようなことを行います:

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

于 2013-07-10T19:19:51.773 に答える