加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 23:34:27 所属栏目:百科 来源:网络整理
导读:Content is not allowed in trailing section. org.xml.sax.SAXParseException: Content is not allowed in trailing section. 总结一下,这个问题主要出现的原因有两点。 1、XML内容不正确,比如多个空格,换行等等。需要仔细排查; 2、使用Stream读取流文


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);

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读