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

前端之家收集整理的这篇文章主要介绍了xml读取异常Invalid byte 1 of 1-byte UTF-8 sequence前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

29627人阅读 评论(6) 收藏 举报

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] view plain copy
  1. org.dom4j.DocumentException:Invalidbyte1of1-byteUTF-8sequence.Nestedexception:Invalid8sequence.
  2. atorg.dom4j.io.SAXReader.read(SAXReader.java:484)
  3. atorg.dom4j.io.SAXReader.read(SAXReader.java:321)
  4. atcom.dataoperate.PaseXml.pXml(PaseXml.java:28)
  5. atcom.dataoperate.JdbcOp.insertDb(JdbcOp.java:30)
  6. atcom.dataoperate.JdbcOp.main(JdbcOp.java:89)
  7. Nestedexception:
  8. com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException:Invalid8sequence.
  9. atcom.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:684)
  10. atcom.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:554)
  11. atcom.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1742)
  12. atcom.sun.org.apache.xerces.internal.impl.XMLEntityScanner.peekChar(XMLEntityScanner.java:487)
  13. atcom.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2687)
  14. atcom.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
  15. atcom.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
  16. atcom.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
  17. atcom.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
  18. atcom.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
  19. atcom.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
  20. atcom.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
  21. atcom.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
  22. 465)
  23. Nestedexception:com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException:Invalid atcom.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:684)
  24. atcom.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:554)
  25. atcom.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1742)
  26. atcom.sun.org.apache.xerces.internal.impl.XMLEntityScanner.peekChar(XMLEntityScanner.java:487)
  27. atcom.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2687)
  28. atcom.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
  29. atcom.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
  30. atcom.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
  31. 808)
  32. 737)
  33. atcom.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
  34. atcom.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
  35. atcom.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
  36. 465)
  37. 321)
  38. atcom.dataoperate.PaseXml.pXml(PaseXml.java:28)
  39. atcom.dataoperate.JdbcOp.insertDb(JdbcOp.java:30)
  40. atcom.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] copy
    SAXReaderreader=newSAXReader();
  1. org.dom4j.Documentdocument=reader.read("D:\\ha.xml");
  2. OutputFormatof=newOutputFormat();
  3. of.setEncoding("UTF-8");//改变编码方式
  4. XMLWriterwriter=newXMLWriter(newFileWriter"d:\\dom4j.xml"),of);

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

copy
    FileInputStreamin=newFileInputStream(newFile(fileName));
  1. Readerread=newInputStreamReader(in,"gbk");
  2. Documentdocument=reader.read(read);
原文链接:https://www.f2er.com/xml/297847.html

猜你在找的XML相关文章