0

I am trying to use ROME to parse an RSS feed like this:

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

However, I cannot find a method to get the "WebMaster" field or any other customized field.

I have read about the custom modules in Rome from here, but I couldn't figure out how to use it. I create a similar SamplleModule, SampleModuleImpl, and SampleModule Parser for webMaster field, but I don't know how to use it!

This the classes that I have implemented: 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;

    }

}

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

}

I have also added these module to rome.properties. I just don't know how to use them in my reader method. Any idea folks?

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
  • where in rome.properties did you add it? Make sure you're setting it for the ModuleParser element for the version of RSS you are trying to parse. – Rick Mangi Jul 10 '13 at 18:11
  • That's not the problem. The question is what should I add to my code to have the "getWebMaster" method? I mean to my first code. – Afshin Moazami Jul 10 '13 at 18:15

1 Answers1

0

take a look here for an example of how do to this with the MRSS module:

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

Basically you take a SyndEntry object and using the namespace for your module you get an instance of your module object from the entry if one exists, so in your case:

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

And then you can use it. I use groovy with rome for my parser and do things like this:

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

Rick Mangi
  • 3,761
  • 1
  • 14
  • 17