I’m developing a Feed RSS parser and I’m stuck on what I should get back from it.
I give as input the feed’s URL and the elements to read (example: title, description…), and I used to get an arraylist of hashtable, where each key is an element:
ArrayList<Hashtable<String,String>>
But I’d like to do things better. Since there are classes in my application which manage the news retrieved from different URLs (classes News1, News2, News3) I’d like to return an ArrayList where each element has an abstract method (its implementation will change depending on News1, 2 or 3) to convert the hashtable into the object’s attributes.
My problem is that I don’t know how to cast every dictionary into an object derived from an astract class…I even tried to create something like this:
public abstract class GenericNews extends Hashtable<String, String> {
public abstract News getObject();
}
but I can’t downcast it. How could I do it?
Thanks for your help.
You won’t be able to cast it. You will need to create a new instance of your GenericNews object which wraps the HashTable, and return that.
Also, I’m not sure it’s wise to actually extend Hashtable in Generic news – why not make your ArrayList an
ArrayList<GenericNews>and have methods on GenericNews to access items within it, such asgetNewsItem(String keyword).