我的解析器跳过元素

时间:2013-06-21 14:55:46

标签: java android xml

我正在尝试制作一个使用网络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 - &gt;频道 - &gt; item - &gt;标题 - &gt;描述......

我很无能为力。请帮忙!

1 个答案:

答案 0 :(得分: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)更改下面的字符(...)方法:

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

[EDIT

3)移动&#39; chars&#39;来自&#39; startElement&#39;到构造函数。即:

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());
    } //... 

EDIT]

更多信息@ Using SAXParser in Android