Reputation: 2304
I'm using ROME library to read RSS feeds with Java.
I'm in the case where I have multiples RSS feeds (all with 2.0 version) and each feed has its own structure and custom tags. I read in the official documentation that we can specify a custom parser when reading the feeds.
I have implemented a custom parser and specified this parser in a rome.properties and it works fine.
My goal as a next step is to specify a custom parser for each feed I'm reading to avoid having a long and complex parser.
But it appears in the documentation that custom parsers defined in the properties file are more intended to be used for different RSS versions rather than different XML structures.
All the classes defined in this property have to implement the com.rometools.rome.io.WireFeedParser interface. Parser instances must be thread-safe. The return value of the getType() method is used as the primary key. If more than one parser returns the same type, the latter one prevails.
The getType()
method here points out to the rss version
I need to know if there is a way (or not) to specify with ROME different custom parsers for different feeds.
Upvotes: 0
Views: 360
Reputation: 113
Made this work by defining a different feed type for each of my custom parser, and overriding isMyType
so that it checks the source link or title of the RSS feed.
public class FooParser extends RSS20Parser {
public FooParser() { super("foo_rss_2.0"); }
@Override
public boolean isMyType(Document document) {
return super.isMyType(document) && isFoo(document);
}
}
Then, you can define a Converter for that feed type.
public class FooConverter extends ConverterForRSS20 {
public FooConverter() { super("foo_rss_2.0"); }
}
You need to register both in rome.properties
WireFeedParser.classes=FooParser
Converter.classes=FooConverter
Somehow I also had re-order how the plugins are loaded by rome
. I found out that I can do this by re-adding RSS20Parser
into the "extra plugin" file.
WireFeedParser.classes=FooParser \
com.rometools.rome.io.impl.RSS20Parser
Converter.classes=FooConverter \
com.rometools.rome.feed.synd.impl.ConverterForRSS20
If you don't do this, RSS20Parser
will be used for all your RSS 2.0 feeds.
Upvotes: 0