博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【BUG】xml读取异常Invalid byte 1 of 1-byte UTF-8 sequence
阅读量:4886 次
发布时间:2019-06-11

本文共 8741 字,大约阅读时间需要 29 分钟。

来自

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解析时遇到的编码异常:

 

[java]
  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重新写一遍

 

[javascript]
  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来读,修改字符编码

 

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

转载于:https://www.cnblogs.com/helloxiaoxiang/p/3748100.html

你可能感兴趣的文章
经典代码(01)
查看>>
生成ico格式图标
查看>>
并查集hdu4424
查看>>
jdbc之分页查询
查看>>
sbrk and coreleft
查看>>
树型DP
查看>>
怎么在ubuntu上使用pidgin登陆QQ
查看>>
思维的惰性
查看>>
【Android】学习记录<1> -- 初识ffmpeg
查看>>
关于IAsyncResult接口的CompletedSynchronously属性
查看>>
编译原理——算符优先分析文法(附源代码)
查看>>
jboss的启动过程
查看>>
9个移动网站优化的最佳实践
查看>>
李昌镐:苍老的青春(转载) 韩国围棋职业棋手
查看>>
JPA 使用报Named query not found错误
查看>>
cocos2d-x3.2中加入Android手机震动
查看>>
css3处理sprite背景图压缩来解决H5网页在手机浏览器下图标模糊的问题
查看>>
EtherCAT Slave 入门教程 - 邮箱服务(1)
查看>>
【poj3537】 Crosses ans Crosses
查看>>
10.04 FZSZ模拟Day1 总结
查看>>