/* This method performs the actual feed parsing and returns a nice Channel-object for easy feed handling. */
public Channel read(){
Channel feed = null;
try{
// Items and the channel have XML elements using the same name.
boolean isFeedHeader = true;
String title = null;
String link = null;
String description = null;
String author = null;
String enclosure = null;
String guid = null;
String language = null;
String copyright = null;
String managingEditor = null;
String webmaster = null;
String pubDate = null;
String source = null;
String lastBuildDate = null;
String category = null;
String generator = null;
String ttl = null;
String image = null;
// Lets create the factories and reader objects first to handle the XML stream
InputStream inputStream = null;
try {
inputStream = uri.openStream();
}
catch (IOException e) {
throw new RuntimeException(e);
}
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(inputStream);
// StAX is event based, so lets loop trough the events and while there is next
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
String localPart = event.asStartElement().getName().getLocalPart();
switch (localPart) {
case _item:
if (isFeedHeader) {
// This is done once to instantiate a channel object and save the channels/feed header/metadata
// This takes place right after the parser has hit the first start tag of an item-element in the RSS-feed
isFeedHeader = false;
feed = new Channel(title, link, description, language, copyright, managingEditor, webmaster, pubDate, lastBuildDate, category, generator, ttl, image);
}
event = eventReader.nextEvent();
break;
case _title:
title = getElementData(event, eventReader);
break;
case _link:
link = getElementData(event, eventReader);
break;
case _description:
description = getElementData(event, eventReader);
break;
case _author:
author = getElementData(event, eventReader);
break;
case _enclosure:
enclosure = getElementData(event, eventReader);
break;
case _guid:
guid = getElementData(event, eventReader);
break;
case _language:
language = getElementData(event, eventReader);
break;
case _copyright:
copyright = getElementData(event, eventReader);
break;
case _managingEditor:
managingEditor = getElementData(event, eventReader);
break;
case _webmaster:
webmaster = getElementData(event, eventReader);
break;
case _pubDate:
pubDate = getElementData(event, eventReader);
break;
case _source:
source = getElementData(event, eventReader);
break;
case _lastBuildDate:
lastBuildDate = getElementData(event, eventReader);
break;
case _category:
category = getElementData(event, eventReader);
break;
case _generator:
generator = getElementData(event, eventReader);
break;
case _ttl:
ttl = getElementData(event, eventReader);
break;
case _image:
image = getElementData(event, eventReader);
break;
}
}
else if (event.isEndElement()) {
/*
* This event happens when the parser hits the closing tag of an RSS-feed element.
* We are interested at item-tags. When they close we want to save the information of the child elements to a new item-object and to the feed itself
*/
if (event.asEndElement().getName().getLocalPart() == (_item)) {
Item item = new Item();
item.setTitle(title);
item.setDescription(description);
item.setLink(link);
item.setAuthor(author);
item.setCategory(category);
item.setEnclosure(enclosure);
item.setGuid(guid);
item.setPubDate(pubDate);
item.setSource(source);
feed.getItems().add(item);
// We have now saved a new item to our feed model so we can continue to the next item
event = eventReader.nextEvent();
continue;
}