Neha
Neha

Reputation: 71

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.

Upvotes: 3

Views: 2314

Answers (2)

wai yu
wai yu

Reputation: 343

You need to write a custom converter to get the data.

part of code same to above.

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

}

Then write the converter.

public class CustomConverter extends ConverterForRSS20 {

        protected SyndEntry createSyndEntry(Item item) {
            List<HashMap<String,String>> temp = new ArrayList<HashMap<String,String>>();  
            SyndEntry syndEntry = super.createSyndEntry(item);
            customItem customItem = (customItem)item;         

            List<String> customList = new ArrayList<String>();
            customList.add( customItem.getCustomString() );
            //set to empty attribute ex foreignmarkup
            syndEntry.setForeignMarkup( customList );

            return syndEntry;
        }   
}

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
# Feed Converter implementation classes
#
Converter.classes=com.example.CustomConverter

Then you can get value.

SyndFeed feed = input.build(new XmlReader(feedUrl));
List<SyndEntryImpl> entrys =  feed.getEntries();
    for(SyndEntryImpl entry:entrys ){
        System.out.println( entry.getForeignMarkup() ); 
    }       

Upvotes: 0

Magnus Gustavsson
Magnus Gustavsson

Reputation: 580

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

Upvotes: 3

Related Questions