我正在尝试使用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字段创建了类似的SamplleModule
,SampleModuleImpl
和SampleModule
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中。 我只是不知道如何在我的读者方法中使用它们。 有什么想法吗?
答案 0 :(得分: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