您的位置:首页 > 编程语言 > Java开发

XML解析问题

2015-12-16 13:48 671 查看
Content is not allowed in trailing section. org.xml.sax.SAXParseException: Content is not allowed in trailing section.

总结一下,这个问题主要出现的原因有两点。

 

1、XML内容不正确,比如多个空格,换行等等。需要仔细排查;

2、使用Stream读取流文件不正确,详细如下:

 

读写文件时,一般采用的是每次从inputStream中read 一个有固定大小的byte array时,这时候在

 

byte[] array = new byte[1024];

 

BufferedInputStream buffInput = new BufferedInputStream(inputStream);

FileOutputStream fileOS = new FileOutputStream(targetFile);

BufferedOutputStream buffOS = new BufferedOutputStream(fileOS);

int word = 0;

while ((word = buffInput.read(array)) != -1){

buffOS.write(array);

}

 

 

上面的写法,如果最后一次read到array中不足1024的时候,前一次read的byte同样会出现在byte[]中,从而导致XML解析失败。因此可以采用下面的方法:

 

 

BufferedInputStream buffInput = new BufferedInputStream(inputStream);

FileOutputStream fileOS = new FileOutputStream(targetFile);

BufferedOutputStream buffOS = new BufferedOutputStream(fileOS);

int word = 0;

while ((word = buffInput.read()) != -1){

buffOS.write(word);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  eclipse