Handle RSS Feeds with Names in Android

I'm trying to write an XML parser that takes an RSS feed and extracts the image urls given in the url attribute of a tag <media:thumbnail>

. This is all done through android.Util.Xml

and is an adaptation of the code shown here . An example of an RSS feed I'm trying to use is the BBC News RSS Feed .

However, the environment is an additional namespace and (possibly) as a result my parser is not working as it should.

Below is a version of my analysis method. Is there some (no doubt simple) way to make my image url list work?

public List<string> parse() {
    URL feedUrl = new URL("http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml");

    InputStream feedStream;

    try {
        feedStream = feedUrl.openConnection().getInputStream();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }              

    final List<string> ret = new ArrayList<string>();

    RootElement root = new RootElement("rss");
    Element channel = root.getChild("channel");
    Element item = channel.getChild("item");

    item.getChild("media", "thumbnail").getChild("url").setEndTextElementListener(new EndTextElementListener() {
        public void end(String body) {
            ret.add(body);
        }
    });

    try {
        Xml.parse(feedStream, Xml.Encoding.UTF_8, root.getContentHandler());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return ret;
}

      

+2


a source to share


3 answers


In one way, I found the Xml parser (as of Froyo 2.2) works with namespace prefixes by specifying the namespace url as the first parameter to your item.getChild () call. For example, if your xml looks like this, your code can use the xmlns url as the first parameter.

<?xml version="1.0" encoding="utf-8"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sample="http://www.example_web_site_here.com/dtds/sample_schema.dtd" version="2.0">
    <channel><item><sample:duration>1:00:00</sample:duration></item></channel></rss>

      

Your listener setup will look like this to get the duration element text:



 item.getChild("http://www.example_web_site_here.com/dtds/sample_schema.dtd", "duration").setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                this.itemDuration = body;
            } });

      

It requires knowledge of the namespace, but it works for me. In my case, I know the namespace.

+5


a source


As far as I can tell, the "android" SAX parser does not support namespace nesting (xmlns) (despite the route object that specifically mentions the namespace), the J2SE stripped SAX parser is also mangled along the way and the DOM parser is weighty. but it works.



I have been using DOM successfully with XML namespaces, but would prefer a SAX solution that doesn't involve adding a working XML library like JDOM to my packages.

0


a source


I would not recommend trying to implement your own RSS parser, but instead used the standard library to do this.

You need to serve all formats RSS 1, RSS 2, Atom, etc. Even then, you will have to struggle with poorly formatted feeds.

I've faced similar problems in the past, so I decided to parse it on the server and just get the parsed content. This allows me to run more complex libraries and a parser that I can modify without preempting an update for my application. You should really strive to keep your application lightweight and push as much logic out of it as you can (onto your own backend server).

I have the following service running on AppEngine which makes parsing XML / JSON much easier. There is a fixed and simple structure for the answer. You can use this to parse

http://evecal.appspot.com/feedParser

You can send POST and GET requests with the following parameters.

feedLink: RSS feed response url: JSON or XML as response format

Examples:

For POST request

curl --data-urlencode "feedLink = http://feeds.bbci.co.uk/news/world/rss.xml" --data-urlencode "response = json" http://evecal.appspot.com/feedParser

For a GET request

evecal.appspot.com/feedParser?feedLink=http://feeds.nytimes.com/nyt/rss/HomePage&response=xml

My Android app also uses "NewsSpeak".

0


a source







All Articles