SAXParser抛出org.apache.harmony.xml.ExpatParser $ ParseException

时间:2011-05-17 08:25:21

标签: android saxparser

当我尝试用android中的SAXParser解析xml时。最后一个'>'不会工作。总是显示错误:

org.apache.harmony.xml.ExpatParser$ParseException: At line 15, column 12: not well-formed (invalid token)

这是我的xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<magazines> 
<magazine id="176"> 
<publising>1</publising> 
<price>0</price> 
<paid_state>True</paid_state> 
<cover_img>http://172.23.5.222/files/images/pub/issue/image-20110517-10bcmaka9bklah8ph57o.jpg</cover_img> 
</magazine> 
<magazine id="175"> 
<publising>1</publising> 
<price>0</price> 
<paid_state>True</paid_state> 
<cover_img>http://172.23.5.222/files/images/pub/issue/image-20110517-9jqtrfo5lozztb7m5xlu.jpg</cover_img> 
</magazine> 
</magazines>

这是我的经纪人

public class BKXMLCoverContentHandler extends DefaultHandler  {

private String TAG = "BKXMLCoverContentHandler";

private TreeMap<Integer, Magazine> magazines;
private Magazine magazine;

private String tempString;

private static final String MAGAZINES = "magazines";
private static final String MAGAZINE = "magazine";
private static final String ID = "id";
private static final String PUBLISHING = "publising";
private static final String PRICE = "price";
private static final String PAID_STATE = "paid_state";
private static final String COVER_IMG = "cover_img";

public TreeMap<Integer, Magazine> getMagazines() {
    return magazines;
}


@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {

    if (magazine != null) {
        String valueString = new String(ch, start, length);
        if (PUBLISHING.equals(tempString)) {
            Log.d(TAG, valueString);
            magazine.setPublishingStatus(valueString);
        } else if (PRICE.equals(tempString)) {
            Log.d(TAG, valueString);
            magazine.setPrice(valueString);
        } else if (PAID_STATE.equals(tempString)){
            Log.d(TAG, valueString);
            magazine.setPaidStatus(tempString);
        } else if (COVER_IMG.equals(tempString)){
            Log.d(TAG, valueString);
            magazine.setCoverUrl(valueString);
        } 
    } 
}

@Override
public void startElement(String uri, String localName, String name,
        Attributes attributes) throws SAXException {
    if (MAGAZINES.equals(localName)){
        Log.d(TAG, localName);
        magazines = new TreeMap<Integer, Magazine>();
    }

    if (MAGAZINE.equals(localName)) {

        magazine = new Magazine();
        Log.d(TAG, localName);
        magazine.setId(new Integer(attributes.getValue(ID)));
    } 
    tempString = localName;
}

@Override
public void endElement(String uri, String localName, String name)
        throws SAXException {
        if(MAGAZINE.equals(localName) && magazine != null){
            Log.d(TAG, localName);
            magazines.put(magazine.getId(), magazine);
            magazine = null;
        } else if(MAGAZINES.equals(localName)){
            Log.d(TAG, localName);
        }
        tempString = null;
}

}

从您可以看到的代码中我记录了每一行。所以我得到了一个日志输出:

D/BKXMLCoverContentHandler(  980): magazines
D/BKXMLCoverContentHandler(  980): magazine
D/BKXMLCoverContentHandler(  980): 1
D/BKXMLCoverContentHandler(  980): 0
D/BKXMLCoverContentHandler(  980): True
D/BKXMLCoverContentHandler(  980): http://172.23.5.222/files/images/pub/issue/image-20110517-10bcmaka9bklah8ph57o.jpg
D/BKXMLCoverContentHandler(  980): magazine
D/BKXMLCoverContentHandler(  980): magazine
D/BKXMLCoverContentHandler(  980): 1
D/BKXMLCoverContentHandler(  980): 0
D/BKXMLCoverContentHandler(  980): True
D/BKXMLCoverContentHandler(  980): http://172.23.5.222/files/images/pub/issue/image-20110517-9jqtrfo5lozztb7m5xlu.jpg
D/BKXMLCoverContentHandler(  980): magazine
D/BKXMLCoverContentHandler(  980): magazines

有人可以说出问题是什么吗?

1 个答案:

答案 0 :(得分:1)

问题已经解决。

我从url读取xml并将其写入我的android设备sdcard。写入时,我每次使用缓冲区读取8 * 1024字节。 xml对于8k来说太短了,所以程序用空白填充文件得到8k字节。导致问题的原因。