Reputation: 726
How to just download the portion of rss?
I need some approach to achieve this. I can understand the "page last modified" to prevent the downloading, but I want to know whether I can just download the new items in an RSS feed or not ?
Upvotes: 0
Views: 301
Reputation: 766
Use the XmlPullParser to parse your content. Once you reach an item with a pubDate older than what you're looking for, break out of the loop. Here's some old code I wrote to do just this.
I eventually discovered that downloading all the RSS feeds I wished to parse and storing them locally was more efficient battery-wise than pulling one after the other and parsing them.
public static List<Content> parseNewEpisodesFromXml(InputStream stream,
long id, long oldPubDate) throws XmlPullParserException,
IOException, ParseException {
List<Content> content = new ArrayList<Content>();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(stream, "UTF-8");
int eventType = xpp.getEventType();
SimpleDateFormat pubDateFormatter = new SimpleDateFormat(
"EEE, dd MMM yyy HH:mm:ss zzzz");
Content content = new Content()
boolean oldContentFound = false;
while (eventType != XmlPullParser.END_DOCUMENT || !oldContentFound) {
name = xpp.getName();
switch (eventType) {
case (XmlPullParser.END_TAG):
// end stuff here
brea;
case (XmlPullParser.START_TAG):
if {
// do your thing here...
} else if (name.matches("pubDate") {
content = xpp.nextText();
Date pubDate = pubDateFormatter.parse(content);
if (oldPubDate == pubDate.getTime()) {
oldContentFound = true;
break;
}
}
break;
}
eventType = xpp.next();
}
}
Upvotes: 1
Reputation: 25028
If you want "just a part" of RSS feed (or by extension, any XML document) you need a pull parser. A pull parser is a type of parser that lets you decide how much of the XML you want parsed. For example, in an RSS feed of a 100 items, you might want to stop parsing after the first 25 items.
The XmlPullParser
is what you need to look into.
Now, back to your situation where you want to download just the new feeds. Yes, you can. Here is how:
.xml
file). For the sake of completeness, there is also a push parser. A push parser is a parser that, once started, keeps parsing the XML until it reaches the end o f file or some exception is generated. SAXParser
is a push parser.
Upvotes: 1