Android XML.parse()找不到内容:编码标签

时间:2012-05-04 16:54:32

标签: android xml rss

我正在使用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);
    }
}

我哪里出错了? :)我可以毫无问题地从<item>元素中检索其他数据,例如title或pubDate;它只是内容:编码让我望而却步。

2 个答案:

答案 0 :(得分:3)

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

您将在rss元素中找到内容命名空间:

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

所以获取Element看起来像这样:

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

答案 1 :(得分:0)

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

相关问题