XStream The problem of Chinese character garbled when deserializing XML

Source: Internet
Author: User
Tags stringbuffer

Recently, in the process of doing the project, we used the XStream technology to realize the conversion between JavaBean and XML.

Do not say nonsense, for XStream learning, in the official website to step by step study, you can learn, but there are some problems will be encountered in the course of practice.

such as Chinese characters garbled problem.

Workaround:

File file= New file ("F://back.xml");

InputStreamReader in = null;
String XML = null;

try {
The main thing here is the setting
in = new InputStreamReader (new FileInputStream (file), Charset.forname ("GBK"));


StringBuffer sb = new StringBuffer ();
Char[] array= new char[1024];
int length =-1;
while (Length=in.read (array)!=-1) {
Sb.append (array, 0, length);
}

In.close ();
Xml=sb.tostring (). Trim ();
SYSTEM.OUT.PRINTLN (XML);
catch (Exception e) {
Todo:handle exception
}

XStream XStream = null;
XStream = new XStream (new Domdriver ());

Xstream.processannotations (Resultcallback.class);
Xstream.autodetectannotations (TRUE);


Resultcallback result= (resultcallback) xstream.fromxml (XML);


System.out.println (Result.getb2bdocumentsid () + "----" +result.getmsg ());



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.