在用XStream将xml 反序列化 为Javabean时报错,信息如下:
com.thoughtworks.xstream.io.StreamException: : Invalid byte 2 of 2-byte UTF-8 sequence.
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://www.micmiu.com/techother/exception/xml-invalid-byte/
解决办法 :
就是为DOM解析器指定好编码utf-8,代码如下:
1 |
XStream xStream = new XStream(new DomDriver("utf-8")); |
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/exception/xstream-invalid-byte-2/
0 条评论。