本文主要是介绍xstream:Invalid byte 2 of 2-byte UTF-8 sequence,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在用XStream将xml 反序列化 为Javabean时报错,信息如下:
at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:88)
at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:70)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:891)
at michael.xstream.XtreamTestMain.main(XtreamTestMain.java:71)
Caused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 2-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:369)
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.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.DOMParser.parse(DOMParser.java:235)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:79)
... 3 more
产生的原因 : 简单的说就是XML文件的编码和解析XML时用的编码不一致产生的问题。
由于文件会以系统的默认编码对文件进行保存,在中文版的window下Java的默认的编码为GBK,所以默认生成的xml文件是以GBK格式来保存的,所以我们使用GBK、GB2312编码来生成xml文件能正确的被解析,而以UTF-8格式生成的文件不能被xml解析器所解析的原因,其实和之前文章碰到的问题类似:http://sjsky.iteye.com/blog/1053931
解决办法 :
就是为DOM解析器指定好编码utf-8,代码如下
XStream xStream = new XStream(new DomDriver("utf-8"));
转载请注明来自:Michael's blog @ http://sjsky.iteye.com
----------------------------- 分 ------------------------------ 隔 ------------------------------ 线 ------------------------------
这篇关于xstream:Invalid byte 2 of 2-byte UTF-8 sequence的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!