解析Android中的RSS Feed描述

时间:2012-05-04 01:36:07

标签: android saxparser

我正在构建一个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代码,如果您需要更多的帖子,但我认为这是我要解决的问题,其余的只是将其发送到活动并查看它。 (我在网页浏览中查看。)

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


}

1 个答案:

答案 0 :(得分:0)

我认为你错过了CDATA中的[和]?

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

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