Search code examples
androidxml-namespacessax

Parse attribute from xmlns based elements with SAX in Android


i'm trying to parse this xml file but i can't find a way to parse the url attribute from
<media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/41447000/jpg/_41447190_exomars66esa.jpg"/> element. I tried with the following startElement method in my handler class

public void startElement(String Uri, String localName, String qName, 
Attributes atts) throws SAXException { 
if (localName.equals("channel")) {
        this.in_channel = true;
}else if (localName.equals("item")) {
        this.in_item = true;
}else if (localName.equals("title")) {
        this.in_title = true;
}else if (localName.equals("media:thumbnail")) {
        String attrValue = atts.getValue("url");
        item.setLink(attrValue);
       }
}

I also tried if (localName.equals("media") but didn't work out
I can parse an element like this<link url="http://achdre.freehostia.com/example.xml"/> but not the one I posted above. thanks in advance


Solution

  • You need to use the qName.

    From the doc:

    localName - The local name (without prefix), or the empty string if Namespace processing is not being performed.

    qName - The qualified name (with prefix), or the empty string if qualified names are not available.

    The local name basically strips off the name space, so if you wanted, you could also do 'thumbnail' which would also match.