今天很偶然的一个机会看到了关于XStream这个东西,由于近来做android和xml 打交道越来越多,所以就耐着心看了看。结果发现还挺方便的,我在论坛搜索了一把,大家对XStream的褒贬不一有的人说他性能不好,有的人说他用起来方便很符合自己的需求。。。。。。
总之:众说纷纭啊!我要说的是,不管他好与坏总有它存在的理由,不然大家也就不会用它了,正因为褒贬不一他才会有进步!开源的东西希望大家一起把他做好吧。。。。
和以前一样先说问题:我的问题是在XStream 输出 的文件中包含有中文,在 从XML文件转化为对象的时候,结果中文读取不了。。。(论坛里说 需要一个 。。。xpp3_min-XXX.jar文件。。我添加上了。。还是不能解决中文问题悲剧。。)
原来的代码如下:
public class Person {
private String name;
private String address;
public Person(String name,String address){
this.name = name;
this.address = address;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Test class
public class Test {
public static void main(String[] args) {
//write file
write();
//read file
read();
}
public static void write(){
XStream xstream = new XStream();
Person joe = new Person("张三", "中国");
try {
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("d:/111.xml"));
xstream.toXML(joe,os); //别忘了关 输出流
} catch (Exception e) {
e.printStackTrace();
}
}
public static void read(){
XStream sm = new XStream();
try {
FileInputStream ops = new FileInputStream("d:\\111.xml");
Person t = (Person)sm.fromXML(ops);
System.out.println(t.getName());
System.out.println(t.getAddress()); //别忘了关输入流
} catch (Exception e) {
e.printStackTrace();
}
}
//这里用来打印。。。inputstrem 的内容的。。。
public static String convertStreamToString(InputStream is) throws IOException{
if (is != null) {
StringWriter writer = new StringWriter();
char[] buffer = new char[1024];
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
return writer.toString();
} else {
return "";
}
}
以上代码 运行。。没有错,但是在console 的结果 却和我们的期望有点差别,那就是 我们的中文。。出现了乱码,该怎么解决呢,其实很简单。。看下面的代码对比一下就知道了。。
public class Test {
public static void main(String[] args) {
//write file
write();
//read file
read();
}
public static void write(){
XStream xstream = new XStream();
Person joe = new Person("张三", "中国");
OutputStreamWriter write =null;
try {
write = new OutputStreamWriter(new FileOutputStream("d:/111.xml"),"UTF-8");
xstream.toXML(joe,os);
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(write != null){
write.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void read(){
XStream sm = new XStream();
try {
;
FileInputStream ops = new FileInputStream("d:\\111.xml");
Person t = (Person)sm.fromXML(ops);
System.out.println(t.getName());
System.out.println(t.getAddress());
} catch (Exception e) {
e.printStackTrace();
}
}
public static String convertStreamToString(InputStream is) throws IOException{
if (is != null) {
StringWriter writer = new StringWriter();
char[] buffer = new char[1024];
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
return writer.toString();
} else {
return "";
}
}
}
你看到了。。就那么一点点。。设置输出文件的编码方式即可~!!很简单吧,我当时也郁闷了半天,还是我旁边的一个同事给提了醒,结果加了文件编码就可以了。。。。。。。。。
这样一来就不会出现问题了!但是再用到XStream的时候会出现不少的问题的,现在还没有仔细的研究XStream 源码是怎么样的,应该很不错吧!!!