美文网首页我爱编程
【转载】解析XML各种异常

【转载】解析XML各种异常

作者: ad8d261a83e4 | 来源:发表于2017-10-22 21:17 被阅读0次

The markup in the document following the root element must be well-formed.

XML是树状结构,一定要有个最外层的标签套住

Invalid byte 1 of 1-byte UTF-8 sequence 异常分析和解决

“org.dom4j.DocumentException: Invalid byte 1 of 1-byte UTF-8 sequence.”异常分析和解决:

分析:

该异常由下面的reader.read(file);语句抛出:

SAXReader reader = new SAXReader();

Document doc = reader.read(file);

产生这个异常的原因是:

所读的xml文件实际是GBK或者其他编码的,而xml内容中却用指定编码为utf-8,所以就报异常了!

解决方法:

在解析XML前,将XML编码为UTF-8。

如:req.setCharacterEncoding("UTF-8");

如:new ByteArrayInputStream(submitDataParam.getBytes("UTF-8"))

Invalid byte 2 of 2-byte UTF-8 sequence 异常分析和解决

原因:

saxReader.read()读取的流中包含中文报错:

解决:

SAXReader saxReader =newSAXReader();

byte[] bytes = requestMsg.getBytes();

InputStream in =newByteArrayInputStream(bytes);

InputStreamReader strInStream =newInputStreamReader(in,"GBK"); //即在读流时指定编码

Document document = saxReader.read(strInStream);

相关文章

网友评论

    本文标题:【转载】解析XML各种异常

    本文链接:https://www.haomeiwen.com/subject/mlmnuxtx.html