I am following the well famous IBM tutorial on parsing an RSS feed. I tested it and I am getting a list of the first item only. In the AndroidSaxFeedParser
, we can see that currentMessage is final which means that it can't be changed, and when I wrote my own implementation I removed the copy()
call from currentMessage
because the compiler doesn't find this method (hence the replicated set of data in my opinion).
public class AndroidSaxFeedParser extends BaseFeedParser {
public AndroidSaxFeedParser(String feedUrl) {
super(feedUrl);
}
public List<Message> parse() {
final Message currentMessage = new Message();
RootElement root = new RootElement("rss");
final List<Message> messages = new ArrayList<Message>();
Element channel = root.getChild("channel");
Element item = channel.getChild(ITEM);
item.setEndElementListener(new EndElementListener(){
public void end() {
// Here, what's copy()?!!
messages.add(currentMessage.copy());
}
});
item.getChild(TITLE).setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
currentMessage.setTitle(body);
}
});
item.getChild(LINK).setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
currentMessage.setLink(body);
}
});
item.getChild(DESCRIPTION).setEndTextElementListener(new
EndTextElementListener(){
public void end(String body) {
currentMessage.setDescription(body);
}
});
item.getChild(PUB_DATE).setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
currentMessage.setDate(body);
}
});
try {
Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8,
root.getContentHandler());
} catch (Exception e) {
throw new RuntimeException(e);
}
return messages;
}
}
So my question is, what's copy()
, am I missing something important here?
Edit Basically, what I want to know is:
- What is
copy()
? and why does it seem to work with everyone but not me? (all the people mentioning the tutorial never said anything about it..)
Also, the reason why I am making it final is because the compiler is asking me to do it. If I remove the final
keyword, I get the following error message:
Cannot refer to a non-final variable currentMessage inside an inner class defiend in a different method.
Thanks!