【问题标题】:parsing RSS Feed Description in Android在 Android 中解析 RSS 提要描述
【发布时间】:2012-05-04 01:36:07
【问题描述】:

我正在构建一个 android 应用程序,我正在尝试解析并且我正在尝试从 rss 提要中解析此描述标签。

<description> 
<![CDATA[
<h1>What Do You Worship?</h1><p>This week <a href="https://twitter.com/#!/nathanielcoon">@Nathanielcoon</a> teaches on the life of Abraham.</a></p> <p><a href="http://www.esvbible.org/search/Genesis+12%3A7-8/">Genesis 12:7-8</a></p> <p><a href="http://www.esvbible.org/search/Genesis+13%3A2-4/">Genes
]]>
<![CDATA[
is 13:2-4</a></p> <p><a href="http://www.esvbible.org/search/Genesis+13%3A14-18/">Genesis 13:14-18</a></p> <p><a href="http://www.esvbible.org/search/Genesis+14%3A17-24/">Genesis 14:17-24</a></p> <p><a href="http://www.esvbible.org/search/Genesis+15%3A1-21/">Genesis 15:1-21</a></p> <p><a href="http://www.esvbible.org/search/Genesis+17%3A3/">Genesis 17:3</a></p> <p><a href="http://www.esvbible.org/search/Genesis+17%3A17/">Genesis 17:17</a></p> <p><a href="http://www.esvbible.org/search/Genesis+18%3A1-33/">Genesis 18:1-33</a></p> <p><a href="http://www.esvbible.org/search/Genesis+21%3A33/">Genesis 21:33</a></p> <p><a href="http://www.esvbible.org/search/Genesis+22%3A4-14/">Genesis 22:4-14</a></p> <p><a href="http://www.esvbible.org/search/Isaiah+44%3A1-20/">Isaiah 44:1-20</a></p> <p><a href="http://www.esvbible.org/search/Revelation+4%3A1-5%3A15/">Revelation 4:1-5:14</a></p> <p><a href="http://www.esvbible.org/search/Isaiah+45%3A20/">Isaiah 45:20</a></p> <p><a href="http://www.esvbible.org/search/Isaiah+45%3A20-22/">Isaiah 45:20-22</a></p> <p><a href="http://www.esvbible.org/search/Matthew+27%3A50-51/">Matthew 27:50-51</a></p> <p><a href="http://www.esvbible.org/search/Mark+15%3A37-38/">Mark 15:37-38</a></p> <p><a href="http://www.esvbible.org/search/Luke+23%3A45-46/">Luke 23:45-46</a></p> <p><a href="http://www.esvbible.org/search/Hebrews+10%3A19/">Hebrews 10:19</a></p> <p><a href="http://www.esvbible.org/search/Isaiah+6%3A1-13/">Isaiah 6:1-13</a></p> <p>For more resources or to contact us visit <a href="http://www.refugecf.com/resources/">RefugeCf.com</a></p> <p>Follow <a href="https://twitter.com/RefugeCF"> @RefugeCF</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script> on Twitter.</p>

但是,我只得到前两行。

<h1>What Do You Worship?</h1><p>This week <a href="https://twitter.com/#!/nathanielcoon">@Nathanielcoon</a> teaches on the life of Abraham.</a></p>

是否有开关或我找不到的东西?我正在为我的 Android 应用程序使用 SAXParser。

还有。我尝试解析/获取 itunes:summary 标签,但它根本不起作用。我想知道为什么。

谢谢。

编辑:这是我的 RSSHandler 代码,如果您需要更多的病态发布它,但我认为这是我要修复它的地方,其余的只是将它发送到一个活动并查看它。 (我在 webview 中查看它。)

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class RSSHandler extends DefaultHandler {

    final int state_unknown = 0;
    final int state_title = 1;
    final int state_description = 2;
    final int state_link = 3;
    final int state_pubdate = 4;
    final int state_guid = 5;
    final int state_subtitle = 6;
    int currentState = state_unknown;

    RSSFeed feed;
    RSSItem item;

    boolean itemFound = false;

    RSSHandler(){
    }

    RSSFeed getFeed(){
        return feed;
    }

    @Override
    public void startDocument() throws SAXException {
        // TODO Auto-generated method stub
        feed = new RSSFeed();
        item = new RSSItem();

    }

    @Override
    public void endDocument() throws SAXException {
        // TODO Auto-generated method stub
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub

        if (localName.equalsIgnoreCase("item")){
            itemFound = true;
            item = new RSSItem();
            currentState = state_unknown;
        }
        else if (localName.equalsIgnoreCase("title")){
            currentState = state_title;
        }
        else if (localName.equalsIgnoreCase("subtitle")){
            currentState = state_subtitle;
        }
        else if (localName.equalsIgnoreCase("description")){
            currentState = state_description;
        }
        else if (localName.equalsIgnoreCase("link")){
            currentState = state_link;
        }
        else if (localName.equalsIgnoreCase("pubdate")){
            currentState = state_pubdate;
        }
        else if (localName.equalsIgnoreCase("guid")){
            currentState = state_guid;
        }
        else{
            currentState = state_unknown;
        }

    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        // TODO Auto-generated method stub
        if (localName.equalsIgnoreCase("item")){
            feed.addItem(item);
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        // TODO Auto-generated method stub

        String strCharacters = new String(ch,start,length);

        if (itemFound==true){
        // "item" tag found, it's item's parameter
            switch(currentState){
            case state_title:
                item.setTitle(strCharacters);
                break;
            case state_subtitle:
                item.setsubTitle(strCharacters);
                break;
            case state_description:
                item.setDescription(strCharacters);
                break;
            case state_link:
                item.setLink(strCharacters);
                break;
            case state_pubdate:
                item.setPubdate(strCharacters);
                break;  
            case state_guid:
                item.setGuid(strCharacters);
                break;  
            default:
                break;
            }
        }
        else{
        // not "item" tag found, it's feed's parameter
            switch(currentState){
            case state_title:
                feed.setTitle(strCharacters);
                break;
            case state_subtitle:
                feed.setsubTitle(strCharacters);
                break;
            case state_description:
                feed.setDescription(strCharacters);
                break;
            case state_link:
                feed.setLink(strCharacters);
                break;
            case state_pubdate:
                feed.setPubdate(strCharacters);
                break;  
            case state_guid:
                feed.setGuid(strCharacters);
                break;  
            default:
                break;
            }
        }

        currentState = state_unknown;
    }


}

【问题讨论】:

    标签: android saxparser


    【解决方案1】:

    我认为您在 CDATA 中缺少 [ 和 ]?

    http://java.sun.com/webservices/reference/tutorials/jaxp/html/sax.html

    SAX Parser 可能有点难以理解,我会在这些方法中添加一个断点,看看它是否按照您的预期进行。

    【讨论】:

    • 我在那个链接中找不到 [ 和 ] 的用法...,但是下半部分没有出现,因为我只得到第一个 CDATA 吗?如果是这样,我如何能够将这两个 CDATA 合二为一?编辑:找到 startCDATA()、endCDATA(),但我不完全确定如何使用它们。
    • 没关系,看起来好像是文档中的错字... 应该没问题。
    • hmm.. 刚刚看到您关于断点的评论,请检查一下。另一个有点相关的问题:saxparser 在打开活动之前会解析所有内容吗?我注意到如果没有足够的服务,活动将需要一段时间才能加载,以至于 android 认为它没有响应。
    • 你是在activity的主线程中下载解析吗?这种事情必须在 Thread 或 AsyncTask 中完成,否则它会“锁定”并且有时会显示无响应的对话框。
    • 啊。是的。这就说得通了。这是我的第一个 android 应用程序,所以我会查一下。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多