JAVA/SAX-使用XML解析器丢失字符

JAVA/SAX-使用XML解析器丢失字符,java,android,xml,parsing,Java,Android,Xml,Parsing,我正在使用SAX解析器在Android应用程序上解析RSS提要的XML文件,有时对项目的pubDate的解析还没有完成 例: 实际发布日期2015年4月2日星期四12:23:41+0000 PubDate解析结果:Thu 以下是我在解析器处理程序中使用的代码: public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

我正在使用SAX解析器在Android应用程序上解析RSS提要的XML文件,有时对项目的pubDate的解析还没有完成

例:

实际发布日期2015年4月2日星期四12:23:41+0000

PubDate解析结果:Thu

以下是我在解析器处理程序中使用的代码:

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if ("item".equalsIgnoreCase(localName)) {
            currentItem = new RssItem(url);
        } else if ("title".equalsIgnoreCase(localName)) {
            parsingTitle = true;
        } else if ("link".equalsIgnoreCase(localName)) {
            parsingLink = true;
        } else if ("pubDate".equalsIgnoreCase(localName)) {
            parsingDate = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if ("item".equalsIgnoreCase(localName)) {
            rssItems.add(currentItem);
            currentItem = null;
        } else if ("title".equalsIgnoreCase(localName)) {
            parsingTitle = false;
        } else if ("link".equalsIgnoreCase(localName)) {
            parsingLink = false;
        } else if ("pubDate".equalsIgnoreCase(localName)) {
            parsingDate = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (parsingTitle) {
            if (currentItem != null) {
                currentItem.setTitle(new String(ch, start, length));
                parsingTitle = false;
            }
        } else if (parsingLink) {
            if (currentItem != null) {
                currentItem.setLink(new String(ch, start, length));
                parsingLink = false;
            }
        } else if (parsingDate) {
            if (currentItem != null) {
                currentItem.setDate(new String(ch, start, length));
                parsingDate = false;
            }
        }
    }

字符丢失是非常随机的,每次运行应用程序时,都会在不同的XML项中发生。

您假设每个元素只有一个字符调用。这不是一个安全的假设。通过1+个字符调用建立字符串,然后在endElement中应用它


或者,最好使用其中任何一个。

谢谢,我最终遵循了字符串生成器的建议,它成功了!