xml读取异常Invalid byte 1 of 1-byte UTF-8 sequence
生活随笔
收集整理的這篇文章主要介紹了
xml读取异常Invalid byte 1 of 1-byte UTF-8 sequence
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
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解析時遇到的編碼異常:
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重新寫一遍
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來讀,修改字符編碼
FileInputStream in = new FileInputStream(new File(fileName)); Reader read = new InputStreamReader(in,"gbk"); Document document = reader.read(read);轉載于:https://my.oschina.net/u/1159401/blog/309925
總結
以上是生活随笔為你收集整理的xml读取异常Invalid byte 1 of 1-byte UTF-8 sequence的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cocos2dx基础篇(29)——屏幕适
- 下一篇: Utils 工具 推送