Xstream: Invalid byte 2 of 2-byte UTF-8 sequence.

Source: Internet
Author: User
Tags xml parser

 

Xstream: Invalid byte 2 of 2-byte UTF-8 sequence.
    Blog type:

  • Exception
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.