Hamedz
Hamedz

Reputation: 726

How to just download the portion of rss

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

Answers (2)

Michael Powell
Michael Powell

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

An SO User
An SO User

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:

  1. When the app is first run you will be saving all the items in the RSS feed to some local storage (possibly SQLite or as an .xml file).
  2. When you next run the app, retrieve the latest item from the local storage.
  3. Connect to the feed URL and start parsing with the pull parser.
  4. Keep adding every new item that you find to your local storage.
  5. Once you hit the latest item you have retrieved from your local storage, stop parsing. You know for certain that you have all the items beyond this point.

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

Related Questions