Home > javafx > JavaFX – Custom Feed Parsers

JavaFX – Custom Feed Parsers


In one of my previous post I have described the use of RSS and ATOM Task to parse the feeds.

Unfortunately many feeds do not strictly comply with the RSS/ATOM format. Example: In some cases the date specified for pubDate element may be in wrong format, or its an invalid date or the information specified is embedded in CDATA instead of TEXT. The parser will fail for such scenarios. Hence we need to implement custom parsers so as to handle the situation.

Below sample reads engadget rss feed..

For Applet mode, click on above image

Both RssTask and AtomTask API provide an attribute – factory – which can be used to specify custom implementation of RSS or ATOM Factory. Users may also customize various elements such as Channel, Item, Entry, Feed by overriding functions such as – fromXML, tryParseDate etc.

Example: In some cases the default parser fails to convert pubDate to valid DateTime instance. So a new attribute – pubDateText – is added so as to save the date as String.


/**
 * Customize Item so as to override fromXML
 */
class CustomItem extends Item {
    
    public-read var pubDateText : String;
    
    override function fromXML(parser: PullParser): Void {
        
        if(parser.event.qname == PUB_DATE) {
            if ((parser.event.type == PullParser.TEXTor
                (parser.event.type == PullParser.CDATA)) {
                pubDateText = stripCDATA(parser.event.text)
            }
        else if(parser.event.qname == TITLE) {
            title = stripCDATA(parser.event.text);
        else {
            super.fromXML(parser);
        }
    }
}

Now we need to implement a custom Factory which will instantiate and return the above CustomItem instance.


/**
 * Customize Factory so as to return CustomItem
 */
class CustomFactory extends Factory {
    public override function newItem(c: Channel: Item {
        CustomItem parent: c channel: c }
    }
}

Above Factory is set in factory attribute of RssTask as shown below.


var rssTask = RssTask {

    // set custom factory
    factory: CustomFactory { }

    location: "http://www.engadget.com/rss.xml"
    onChannel: function(channel) {  }
    onItem: function(item) { }
}
rssTask.start();

Update: Please refer to SpeedReaderFX by Jim Weaver for real world example.

Try it out and let me know feedback

var dzone_style = ‘2’;

Advertisements
Categories: javafx Tags: , , , ,
  1. September 15, 2009 at 4:47 PM

    I will definitely try it out and give you some feedback.

  2. Noushad T.M
    September 15, 2009 at 10:38 PM

    Oh Rakesh great

  3. September 17, 2009 at 3:21 AM

    @Emma, Noushad Thanks! πŸ™‚

  4. September 29, 2009 at 10:01 PM

    It is really usefull. This documentation is missing in our API Docs.
    These needs to goto our testbase.
    Good post.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: