重解析xml后出错:Content is not allowed in trailing section.


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解析失败。因此可以采用下面的方法

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

buffOS.write(word);

}

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念