Xstream的序列化与反序列化

Xstream插件可以将对象序列化成xml格式的文件;也可以将xml文件转换成对象

反序列化:注意Xtream不能解析KhExport类的getter和setter,所以成员不能定义成私有的,不然会抛出异常;

导入xml文件

File file = new File(entity.getPath());
if (file.exists()) {
    InputStream inS=new FileInputStream(file);
    KhExport arrImport=null;

    XStream xstream = new XStream(new DomDriver()) ;
    //这个information标识一定要和Xml中的保持一直,否则会报错
    xstream.alias("information", KhExport.class);
    xstream.alias("client",KhExportData.class);

    arrImport = (KhExport) xstream.fromXML(inS);

序列化:

XStream stream = new XStream(new DomDriver());
stream.alias("information", KhExport.class);
stream.alias("client",KhExportData.class);

OutputStream outputStream = new FileOutputStream(mFile);
stream.toXML(kh, outputStream);

你可能感兴趣的:(Android)