来自http://blog.csdn.net/chenyanbo/article/details/6866941

xml读取异常Invalid byte 1 of 1-byte UTF-8 sequence

说简单点当你解析别人的xml格式出现这个错误可能就是别人在生成xml时没有保存为utf-8的字符编码格式。

在中文版的window下java的默认的编码为GBK,也就是所虽然我们标识了要将xml保存为utf-8格式但实际上文件是以GBK格式来保存的,所以这也就是为什么能够我们使用GBK、GB2312编码来生成xml文件能正确的被解析,而以UTF-8格式生成的文件不能被xml解析器所解析的原因。

xml解析时遇到的编码异常:

  1. org.dom4j.DocumentException: Invalid byte 1 of 1-byte UTF-8 sequence. Nested exception: Invalid byte 1 of 1-byte UTF-8 sequence.
  2. at org.dom4j.io.SAXReader.read(SAXReader.java:484)
  3. at org.dom4j.io.SAXReader.read(SAXReader.java:321)
  4. at com.dataoperate.PaseXml.pXml(PaseXml.java:28)
  5. at com.dataoperate.JdbcOp.insertDb(JdbcOp.java:30)
  6. at com.dataoperate.JdbcOp.main(JdbcOp.java:89)
  7. Nested exception:
  8. com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
  9. at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:684)
  10. at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:554)
  11. at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1742)
  12. at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.peekChar(XMLEntityScanner.java:487)
  13. at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2687)
  14. at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
  15. at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
  16. at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
  17. at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
  18. at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
  19. at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
  20. at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
  21. at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
  22. at org.dom4j.io.SAXReader.read(SAXReader.java:465)
  23. at org.dom4j.io.SAXReader.read(SAXReader.java:321)
  24. at com.dataoperate.PaseXml.pXml(PaseXml.java:28)
  25. at com.dataoperate.JdbcOp.insertDb(JdbcOp.java:30)
  26. at com.dataoperate.JdbcOp.main(JdbcOp.java:89)
  27. Nested exception: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
  28. at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:684)
  29. at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:554)
  30. at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1742)
  31. at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.peekChar(XMLEntityScanner.java:487)
  32. at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2687)
  33. at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
  34. at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
  35. at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
  36. at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
  37. at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
  38. at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
  39. at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
  40. at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
  41. at org.dom4j.io.SAXReader.read(SAXReader.java:465)
  42. at org.dom4j.io.SAXReader.read(SAXReader.java:321)
  43. at com.dataoperate.PaseXml.pXml(PaseXml.java:28)
  44. at com.dataoperate.JdbcOp.insertDb(JdbcOp.java:30)
  45. at com.dataoperate.JdbcOp.main(JdbcOp.java:89)
org.dom4j.DocumentException: Invalid byte 1 of 1-byte UTF-8 sequence. Nested exception: Invalid byte 1 of 1-byte UTF-8 sequence.
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.dom4j.io.SAXReader.read(SAXReader.java:321)
at com.dataoperate.PaseXml.pXml(PaseXml.java:28)
at com.dataoperate.JdbcOp.insertDb(JdbcOp.java:30)
at com.dataoperate.JdbcOp.main(JdbcOp.java:89)
Nested exception:
com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:684)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:554)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1742)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.peekChar(XMLEntityScanner.java:487)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2687)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.dom4j.io.SAXReader.read(SAXReader.java:321)
at com.dataoperate.PaseXml.pXml(PaseXml.java:28)
at com.dataoperate.JdbcOp.insertDb(JdbcOp.java:30)
at com.dataoperate.JdbcOp.main(JdbcOp.java:89)
Nested exception: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:684)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:554)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1742)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.peekChar(XMLEntityScanner.java:487)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2687)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.dom4j.io.SAXReader.read(SAXReader.java:321)
at com.dataoperate.PaseXml.pXml(PaseXml.java:28)
at com.dataoperate.JdbcOp.insertDb(JdbcOp.java:30)
at com.dataoperate.JdbcOp.main(JdbcOp.java:89)

解决:

1、最简单就是把<?xml version="1.0" encoding="UTF-8"?>改成<?xml version="1.0" encoding="gbk"?>

2、或者把xml打开另存的时候把字符集改为UTF-8后保存

3、在代码解析的时候先把xml重新写一遍

  1. SAXReader reader = new SAXReader();
  2. org.dom4j.Document document = reader.read("D:\\ha.xml");
  3. OutputFormat of = new OutputFormat();
  4. of.setEncoding("UTF-8"); //改变编码方式
  5. XMLWriter writer = new XMLWriter(new FileWriter "d:\\dom4j.xml"), of);
 SAXReader reader = new SAXReader();
org.dom4j.Document document = reader.read("D:\\ha.xml");
OutputFormat of = new OutputFormat();
of.setEncoding("UTF-8"); //改变编码方式
XMLWriter writer = new XMLWriter(new FileWriter "d:\\dom4j.xml"), of);

4、直接dom4j读取的时候用io来读,修改字符编码

  1. FileInputStream in = new FileInputStream(new File(fileName));
  2. Reader read = new InputStreamReader(in,"gbk");
  3. Document document = reader.read(read);

【BUG】xml读取异常Invalid byte 1 of 1-byte UTF-8 sequence的更多相关文章

  1. Xml读取异常--Invalid byte 1 of 1-byte UTF-8 sequence

    xml读取异常Invalid byte 1 of 1-byte UTF-8 sequence org.dom4j.DocumentException: Invalid byte 1 of 1-byte ...

  2. mybatis异常invalid comparison: java.util.Date and java.lang.String

    原文链接:http://blog.csdn.net/wanghailong_qd/article/details/50673144 mybatis异常invalid comparison: java. ...

  3. xml 读取递归算法

    xml 读取递归算法:

  4. paip.获取proxool的配置 xml读取通过jdk xml 初始化c3c0在代码中总结

    paip.获取proxool的配置  xml读取通过jdk xml 初始化c3c0在代码中  xml读取通过jdk xml 初始化c3c0在代码中.. ... 作者Attilax  艾龙,  EMAI ...

  5. Java基础知识强化之IO流笔记27:FileInputStream读取数据一次一个字节数组byte[ ]

    1. FileInputStream读取数据一次一个字节数组byte[ ]  使用FileInputStream一次读取一个字节数组: int read(byte[]  b) 返回值:返回值其实是实际 ...

  6. Linq to XML 读取XML 备忘笔记

    本文转载:http://www.cnblogs.com/infozero/archive/2010/07/13/1776383.html Linq to XML 读取XML 备忘笔记 最近一个项目中有 ...

  7. C#基础笔记---浅谈XML读取以及简单的ORM实现

    背景: 在开发ASP.NETMVC4 项目中,虽然web.config配置满足了大部分需求,不过对于某些特定业务,我们有时候需要添加新的配置文件来记录配置信息,那么XML文件配置无疑是我们选择的一个方 ...

  8. python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multibyte sequence

    python读取文件时提示"UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal m ...

  9. svn checkout The XML response contains invalid XML

    svn checkout 报错:The XML response contains invalid XML 待解决? ---目前没有找到好的解决方法,svn数据库中存的log入手应该可以,有时间再去看 ...

随机推荐

  1. [luogu5008]逛庭院

    首先我们看到数据范围.妈耶!数据这么大,一开始还想用个DP来做,但是看着就不行,那么根据这个数据范围,我们大致可以猜到这道题的算法是一个贪心,那么我们怎么贪呢? 我们首先还是先画一个图: 样例解释一下 ...

  2. google gson

    发现了google的gson,因为之前对于protocolbuf有一些了解,带着一些好奇心,我开始使用了gson. 经过比较,gson和其他现有java json类库最大的不同时gson需要序列化得实 ...

  3. Luogu 1080 【NOIP2012】国王游戏 (贪心,高精度)

    Luogu 1080 [NOIP2012]国王游戏 (贪心,高精度) Description 恰逢H国国庆,国王邀请n位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己 ...

  4. 【SPOJ283】Naptime

    题目大意:给定一个由 N 个点组成的环,点有点权,现从中选出 M 个点,对于顺时针方向来说,每一段被选取的第一个点的点权不计入答案贡献,求选出的最大权值是多少. 题解:首先考虑线性的情况,设 \(dp ...

  5. 【模板】spfa

    代码如下 #include <bits/stdc++.h> using namespace std; const int maxv=1e4+10; const int maxe=5e5+1 ...

  6. MATLAB:增加噪声,同时多次叠加噪声图和原图以及求平均图像(imnoise,imadd函数)

    本次涉及了对原图像增加高斯噪声.多次叠加原图和高斯噪声图以及叠加后的平均图像. close all; %关闭当前所有图形窗口,清空工作空间变量,清除工作空间所有变量 clear all; clc; R ...

  7. R语言:多个因变量时,如何在plot函数中画多条曲线(plot,points,lines,legend函数)

    最近阅读一篇文献<Regional and individual variations in the function of the human eccrine sweat gland>, ...

  8. theano使用

    一  theano内置数据类型 只有thenao.shared()类型才有get_value()成员函数(返回numpy.ndarray)? 1. 惯常处理 x = T.matrix('x') # t ...

  9. Adobe Premiere Pro CC ------ 快捷键

    ctrl + ~:全屏 Esc:退出全屏

  10. linux命令总结iostat命令

    简介 iostat主要用于监控系统设备的IO负载情况,iostat首次运行时显示自系统启动开始的各项统计信息,之后运行iostat将显示自上次运行该命令以后的统计信息.用户可以通过指定统计的次数和时间 ...