I’m trying to write an XML parser that takes an RSS feed & fetches the image urls shown in the url attribute of the <media:thumbnail> tag. This is all being done via android.Util.Xml, & is an adaptation of the code shown here. An example RSS feed that I’m trying to use is the BBC News RSS feed.
However, media is an additional namespace & (probably) as a result my parser isn’t working as it should.
A version of my parse method is below. Is there any (no doubt simple) way to get my list of image URLs working?
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;
}
One way I found that the Xml parser (on Froyo 2.2) works with namespace prefixes is by specifying the namespace URL as the first parameter to your item.getChild() call. For example, if your xml looks like this, you code can use the xmlns url as the first parameter.
Your listener setup would look like this to get the duration element text:
It requires knowledge of the namespace, but it has been working for me. In my case, I know the name space.