Xstream: Invalid byte 2 of 2-byte UTF-8 sequence.
Javaxml
Blog migration to: http://www.micmiu.com
When xstream is used to convert XML
An error is reported when deserialization is set to JavaBean. The information is as follows:
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
Cause:
Simply put, the encoding of XML files is inconsistent with the encoding used for parsing XML files.
Because the file will be saved with the system's default encoding, Java's default encoding in windows of the Chinese version is GBK, so the default generated XML file is saved in GBK format, so we use GBK, gb2312 encoding to generate xml files can be correctly parsed, and the file generated in UTF-8 format cannot be parsed by the XML parser, in fact, it is similar to the problem encountered in the previous article: http://sjsky.iteye.com/blog/1053931
Solution:
It is to specify UTF-8 encoding for the DOM parser. The Code is as follows:
1.XStream xStream = new XStream(new DomDriver("utf-8"));
For instructions on xstream serialization of Java objects for XML and deserialization, see: http://sjsky.iteye.com/blog/784434