【问题标题】:Parse attribute from xmlns based elements with SAX in Android在 Android 中使用 SAX 从基于 xmlns 的元素中解析属性
【发布时间】:2011-03-09 01:43:28
【问题描述】:

我正在尝试解析这个xml file,但我找不到从
<media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/41447000/jpg/_41447190_exomars66esa.jpg"/> 元素解析 url 属性的方法。我在处理程序类中尝试了以下startElement 方法

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);
       }
}

我也试过if (localName.equals("media"),但没有成功
我可以解析像 this<link url="http://achdre.freehostia.com/example.xml"/> 这样的元素,但不能解析我在上面发布的元素。提前谢谢

【问题讨论】:

    标签: android xml-namespaces sax


    【解决方案1】:

    您需要使用 qName。

    来自doc

    localName - 本地名称(不带前缀),如果没有执行命名空间处理,则为空字符串。

    qName - 限定名称(带前缀),如果限定名称不可用,则为空字符串。

    本地名称基本上去除了名称空间,所以如果你愿意,你也可以做 'thumbnail' 这也匹配。

    【讨论】:

    • @demian83 如果你写 thumbnail 而不是 '(localName.equals("media:thumbnail"))' ,它将起作用。
    猜你喜欢
    • 1970-01-01
    • 2012-12-19
    • 2010-12-29
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多