0

我正在尝试制作一个使用来自网络的 XML 的 RSS 阅读器。由于某种原因,它只读取最后一个元素。

这几乎是 XML 文件:

<rss version="2.0">
    <channel>
        <item>
            <mainTitle>...</mainTitle>
            <headline>
                <title>...</title>
                <description>...</description>
                <subTitle>...</subTitle>
                <link>...</link>
            </headline>

            <headline>
                <title>...</title>
                <description>...</description>
                <subTitle>...</subTitle>
                <link>...</link>
            </headline>
        </item>

        <item>
            <mainTitle>...</mainTitle>
            <headline>
                <title>...</title>
                <description>...</description>
                <subTitle>...</subTitle>
                <link>...</link>
            </headline>

            <headline>
                <title>...</title>
                <description>...</description>
                <subTitle>...</subTitle>
                <link>...</link>
            </headline>
        </item>
    </channel>
</rss>

这是解析器:

public class RssHandler extends DefaultHandler {

    // Feed and Article objects to use for temporary storage
    private Article currentArticle = new Article();
    private List<Article> articleList = new ArrayList<Article>();

    // Number of articles added so far
    private int articlesAdded = 0;

    // Number of articles to download
    private static final int ARTICLES_LIMIT = 20;

    // Current characters being accumulated
    StringBuffer chars = new StringBuffer();

    // Current characters being accumulated
    int cap = new StringBuffer().capacity();

    // Basic Booleans
    private boolean wantedItem = false;
    private boolean wantedHeadline = false;
    private boolean wantedTitle = false;

    public List<Article> getArticleList() {
        return articleList;
    }

    public Article getParsedData() {
        return this.currentArticle;
    }

    public RssHandler() {
        this.currentArticle = new Article();
    }

    public void startElement(String uri, String localName, String qName,
            Attributes atts) {
        chars = new StringBuffer();

    }

    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        if (localName.equalsIgnoreCase("title")) {
            currentArticle.setTitle(chars.toString());
        } else if (localName.equalsIgnoreCase("subtitle")) {
            currentArticle.setDescription(chars.toString());
        } else if (localName.equalsIgnoreCase("pubdate")) {
            currentArticle.setPubDate(chars.toString());
        } else if (localName.equalsIgnoreCase("guid")) {
            currentArticle.setGuid(chars.toString());
        } else if (localName.equalsIgnoreCase("author")) {
            currentArticle.setAuthor(chars.toString());
        } else if (localName.equalsIgnoreCase("link")) {
            currentArticle.setEncodedContent(chars.toString());
        } else if (localName.equalsIgnoreCase("item")) 

        // Check if looking for article, and if article is complete
        if (localName.equalsIgnoreCase("item")) {

            articleList.add(currentArticle);

            currentArticle = new Article();

            // Lets check if we've hit our limit on number of articles
            articlesAdded++;
            if (articlesAdded >= ARTICLES_LIMIT) {
                throw new SAXException();
            }
        }
        chars.setLength(0);
    }
    public void characters(char ch[], int start, int length) {
        chars.append(ch, start, length);

    }
}

每当我调试应用程序时,qName 都不是 Item 的直接子代。

它读取 rss -> 频道 -> 项目 -> 标题 -> 描述 ...

我一无所知。请帮忙!

4

1 回答 1

0

1)在 endElement() 方法结束时,您没有重置字符长度,即

   public void endElement(String uri, String localName, String qName)
        throws SAXException {

    //...           
    //Reset 'chars' length at the end always.
    chars.setLength(0);
    }

2) 改变你的 characters(...) 方法,如下所示:

    public void characters(char ch[], int start, int length) {
        chars.append(ch, start, length);
    }

[编辑

3) 将“chars”的初始化从“startElement”移动到构造函数。IE:

public RssHandler() {
    this.currentArticle = new Article();
    //Add here..
    chars = new StringBuffer();
}

和,

public void startElement(String uri, String localName, String qName,
        Attributes atts) {
    //Remove below line..
    //chars = new StringBuffer();
}

4)最后,使用qName而不是localName来检查匹配的标签,即

    if (qName.equalsIgnoreCase("title")) {
        currentArticle.setTitle(chars.toString().trim());
    } else if (qName.equalsIgnoreCase("subtitle")) {
        currentArticle.setDescription(chars.toString().trim());
    } //... 

编辑]

更多信息@在 Android 中使用 SAXParser

于 2013-06-21T16:00:19.377 回答