【发布时间】:2011-02-11 14:19:45
【问题描述】:
我正在尝试编写一个 XML 解析器,它接受 RSS 提要并获取 <media:thumbnail> 标记的 url 属性中显示的图像 url。这一切都是通过android.Util.Xml 完成的,并且是the code shown here 的改编版。我尝试使用的一个示例 RSS 提要是 BBC News RSS feed。
但是,媒体是一个额外的命名空间&(可能)因此我的解析器无法正常工作。
下面是我的解析方法的一个版本。是否有任何(无疑是简单的)方法可以让我的图片 URL 列表正常工作?
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;
}
【问题讨论】:
标签: android rss namespaces xml-namespaces