I am using the following code to parse an XML file. Everything works fine except when I try to read the url attribute from the enclosure tag. Everything I've read shows to use attributes.getValue("url") but I still seem to get a null value back. Perhaps I'm not returning it correctly?
The specific line I'm trying to parse is like this:
This is the only info I see in LogCat when I try to play the mp3 using a button. I/StagefrightPlayer( 33): setDataSource('') E/MediaPlayer( 275): start called in state 4 E/MediaPlayer( 275): error (-38, 0) E/MediaPlayer( 275): Error (-38,0)
The lines I believe are causing the problem are at the bottom: else if (localName.equalsIgnoreCase(ENC)) { String link = attributes.getValue("url"); currentMessage.setEnc(link);
Thanks.
package com.Demo;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
import static com.Demo.BaseFeedParser.*;
public class RssHandler extends DefaultHandler{
private List<Message> messages;
private Message currentMessage;
private StringBuilder builder;
public List<Message> getMessages(){
return this.messages;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
builder.append(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
super.endElement(uri, localName, name);
if (this.currentMessage != null){
if (localName.equalsIgnoreCase(TITLE)){
currentMessage.setTitle(builder.toString());
} else if (localName.equalsIgnoreCase(LINK)){
currentMessage.setLink(builder.toString());
} else if (localName.equalsIgnoreCase(DESCRIPTION)){
currentMessage.setDescription(builder.toString());
} else if (localName.equalsIgnoreCase(PUB_DATE)){
currentMessage.setDate(builder.toString());
} else if (localName.equalsIgnoreCase(SUMMARY)) {
currentMessage.setSummary(builder.toString());
} else if (localName.equalsIgnoreCase(ITEM)){
messages.add(currentMessage);
builder.setLength(0);
}
}
}
@Override
public void startDocument() throws SAXException {
super.startDocument();
messages = new ArrayList<Message>();
builder = new StringBuilder();
}
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, name, attributes);
if (localName.equalsIgnoreCase(ITEM)){
this.currentMessage = new Message();
}
else if (localName.equalsIgnoreCase(ENC)) {
String link = attributes.getValue("url");
currentMessage.setEnc(link);
}
}
}
This call
String link = attributes.getValue("url");
needs the argument to be the XML qualified (prefixed) name, and "url" may just be the local name.
To debug this, you might look at all attributes using getLength() to figure out how many there are and then use getLocalName(int index) and getQName(int index) to show the different forms of the names.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With