Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse custom rss tags using Rome API

I am trying to use Rome for parsing some rss feeds. One of the rss feeds says specifies 0.91 as the version and no custom xml namespace is defined but the entries still have a custom element in them. Can I use Rome to parse such custom tags without any defined namespace?

Thanks.

like image 665
Neha Avatar asked Nov 28 '25 17:11

Neha


1 Answers

Yes. You need to write a custom parser to do it.

Let's say you want to handle the elements customString and customDate. Start by extending the Item class to store the custom elements.

package com.example;

import com.sun.syndication.feed.rss.Item;
import java.util.Date;

public class CustomItem extends Item {

    private String _customString;
    private Date _customDate;

    public String getCustomString() {
        return _customString;
    }

    public void setCustomString(String customString) {
        _customString = customString;
    }

    public Date getCustomDate() {
        return _customDate;
    }

    public void setCustomDate(Date customDate) {
        _customDate = customDate;
    }

}

Then write the parser. You also need to handle any standard elements you want to parse.

package com.example;

import com.example.CustomItem;    
import com.sun.syndication.feed.rss.Item;
import com.sun.syndication.io.WireFeedParser;
import com.sun.syndication.io.impl.DateParser;
import com.sun.syndication.io.impl.RSS091UserlandParser;
import org.jdom.Element;

public class CustomParser extends RSS091UserlandParser implements WireFeedParser {

    public CustomItem parseItem(Element rssRoot, Element eItem) {
        CustomItem customItem = new CustomItem();

        // Standard elements
        Item standardItem = super.parseItem(rssRoot, eItem);
        customItem.setTitle(standardItem.getTitle());
        customItem.setDescription(standardItem.getDescription());

        // Non-standard elements
        Element e = eItem.getChild("customString", getRSSNamespace());
        if (e != null) {
            customItem.setCustomString(e.getText());
        }

        e = eItem.getChild("customDate", getRSSNamespace());
        if (e != null) {
            customItem.setCustomDate(DateParser.parseDate(e.getText()));
        }

        return customItem;
    }

}

Finally you need to define your parser in a rome.properties file along with parsers for any other type of feed you want to handle.

# Feed Parser implementation classes
#
WireFeedParser.classes=com.example.CustomParser
like image 101
Magnus Gustavsson Avatar answered Dec 01 '25 15:12

Magnus Gustavsson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!