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 ());