Sim*_*ice 2 rss android namespaces xml-namespaces
我正在尝试编写一个XML解析器,它采用RSS提要并获取<media:thumbnail>标记的url属性中显示的图像URL .这一切都是通过完成android.Util.Xml,并且是对此处所示代码的修改.我正在尝试使用的示例RSS源是BBC新闻RSS源.
但是,媒体是一个额外的命名空间&(可能)因此我的解析器不能正常工作.
我的解析方法的一个版本如下.有没有(毫无疑问是简单的)方法来获取我的图像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;
}
Run Code Online (Sandbox Code Playgroud)
我发现Xml解析器(在Froyo 2.2上)与名称空间前缀一起使用的一种方法是将名称空间URL指定为item.getChild()调用的第一个参数.例如,如果您的xml看起来像这样,您的代码可以使用xmlns url作为第一个参数.
<?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>
Run Code Online (Sandbox Code Playgroud)
您的侦听器设置看起来像这样以获取持续时间元素文本:
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;
} });
Run Code Online (Sandbox Code Playgroud)
它需要知道命名空间,但它一直在为我工作.就我而言,我知道名称空间.
| 归档时间: |
|
| 查看次数: |
3537 次 |
| 最近记录: |