xstream:Invalid byte 2 of 2-byte UTF-8 sequence

來源:互聯網
上載者:User

 

xstream:Invalid byte 2 of 2-byte UTF-8 sequence
    部落格分類:

  • Exception
JavaXML  
blog遷移至 :http://www.micmiu.com

 

在用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://sjsky.iteye.com/blog/1053931

 

 
解決辦法

 

    就是為DOM解析器指定好編碼utf-8,代碼如下

1.XStream xStream = new XStream(new DomDriver("utf-8"));  

 有關XStream序列化JAVA對象為XML以及還原序列化的使用說明可參見 :    http://sjsky.iteye.com/blog/784434

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.