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);
}
总结一下,这个问题主要出现的原因有两点。
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);
}
相关文章推荐
- MyEclipse Web Project转Eclipse Dynamic Web Project
- Windows Clang开发环境备忘
- 傻瓜式用Eclipse连接MySQL数据库
- Eclipse引入jquery报错如何解决
- 让Flex Builder 3.0与Eclipse3.4整合起来
- Eclipse配置Javascript开发环境图文教程
- 使用zend studio for eclipse不能激活代码提示功能的解决办法
- 图解Eclipse j2ee开发环境的搭建过程
- 两种Eclipse部署动态web项目方法
- ext for eclipse插件安装方法
- 解决Fedora14下eclipse进行android开发,ibus提示没有输入窗口的方法详解
- eclipse导入jquery包后报错的解决方法
- Eclipse 格式化代码时不换行与自动换行的实现方法
- Eclipse下jQuery文件报错出现错误提示红叉
- eclipse如何忽略js文件报错(附图)
- eclipse3.2.2 + MyEclipse5.5 + Tomcat5.5.27 配置数据库连接池
- jquery库或JS文件在eclipse下报错问题解决方法
- 什么是eclipse,eclipse的意思解析
- Zend Studio for Eclipse的java.lang.NullPointerException错误的解决方法
- eclipse+myeclipse 环境配置方法