【问题标题】:Android XML.parse() not finding content:encoded tagAndroid XML.parse() 找不到内容:编码标签
【发布时间】:2012-05-04 16:54:31
【问题描述】:

我正在使用 Android 的 XML 解析助手 (android.util.Xml.parse(InputStream, Encoding, ContentHandler) 来解析这个 RSS 提要:http://www.rssweather.com/wx/nz/christchurch/rss.php。它似乎无法识别 content:encoded 标签,其中包含我要检索的数据。下面是 XML 的结构:

<rss>
    <channel>
        <item>
            <title>...</title>
            <pubDate>...</pubDate>
            // Other tags
            <content:encoded>
                <![CDATA[
                    (.... this is what I want to retrieve ...)
                ]]>
            </content:encoded>
        </item>
    </channel>
</rsss>

这是我的代码:

void parse(){
    RootElement root = new RootElement("rss");
    Element channel = root.getChild("channel");
    Element item = channel.getChild("item");
    Element contentEncoded = item.getChild("content", "encoded");
    // Have also tried these two:
    Element encoded = item.getChild("encoded");
    Element content = item.getChild("content");

    contentEncoded.setEndTextElementListener(new EndTextElementListener() {
        public void end(String body) {
            Log.d("Test", "Content found: " + body);
        }
    });


    try {
        Xml.parse(feedUrl.openConnection().getInputStream(),
                Xml.Encoding.UTF_8, root.getContentHandler());
    } catch (Exception e){
        throw new RuntimeException(e);
    }
}

我哪里做错了? :) 我可以轻松地从 &lt;item&gt; 元素中检索其他数据,例如标题或 pubDate;只有内容:编码让我无法理解。

【问题讨论】:

标签: android xml rss


【解决方案1】:

您可以坚持使用您的 Android Sax Parser。我遇到了同样的问题以及使用内容命名空间和“编码”作为元素的解决方案。

你会在 rss 元素中找到内容命名空间:

<rss version="2.0"
    xmlns:content="http://purl.org/rss/1.0/modules/content/" ... >

所以获取元素看起来像这样:

item.getChild("http://purl.org/rss/1.0/modules/content/", "encoded");

【讨论】:

  • 很抱歉没有早点看到你的答案;我几个月前完成了那个特定的项目。我刚回来遇到类似的问题,并决定再次尝试使其正常工作。这是正确的解决方案。 (没有注意到页面顶部的 xmlns:content 感觉很傻!)
【解决方案2】:

...我认为“内容:编码”不是元素的有效名称。如果“内容”是一个命名空间并且“编码”了这个命名空间的一个元素,那么它将是有效的。

【讨论】:

  • 不幸的是,这是 RSS 提要给我的,所以无论如何我都必须使用它。但是,正如代码所示,我确实尝试使用命名空间“内容”和元素“编码”来检索它,但没有成功。我认为这是 Android 的 SAX 解析助手类的一个特定问题,因为我的答案中链接的 DOM 方法工作正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-26
  • 1970-01-01
相关资源
最近更新 更多