一:理论知识
使用 XStream 不用任何映射就能实现多数 Java 对象的序列化。在生成的 XML 中对象名变成了元素名,类中的字符串组成了 XML 中的元素内容。使用 XStream 序列化的类不需要实现 Serializable 接口。XStream 是一种序列化工具而不是数据绑定工具,就是说不能从 XML 或者 XML Schema Definition (XSD) 文件生成类。
和其他序列化工具相比,XStream 有三个突出的特点:
- XStream 不关心序列化/逆序列化的类的字段的可见性。
- 序列化/逆序列化类的字段不需要 getter 和 setter 方法。
- 序列化/逆序列化的类不需要有默认构造函数。
不需要修改类,使用 XStream 就能直接序列化/逆序列化任何第三方类。
二:下载包
xstream-1.3.1.jar
三:
package com.samples; public class Employee { private String name; private String designation; private String department; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesignation() { return designation; } public void setDesignation(String designation) { this.designation = designation; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } @Override public String toString() { return "Name : "+this.name+ "\nDesignation : "+this.designation+ "\nDepartment : "+this.department; } } |
清单 2. Writer.java 序列化对象
package com.samples; import java.io.FileNotFoundException; import java.io.FileOutputStream; import com.thoughtworks.xstream.*; public class Writer { public static void main(String[] args) { Employee e = new Employee(); //Set the properties using the setter methods //Note: This can also be done with a constructor. //Since we want to show that XStream can serialize //even without a constructor, this approach is used. e.setName("Jack"); e.setDesignation("Manager"); e.setDepartment("Finance"); //Serialize the object XStream xs = new XStream(); //Write to a file in the file system try { FileOutputStream fs = new FileOutputStream("c:/temp/employeedata.txt"); //注意别名的运用,此处不使用别名,Employee 在xml中会以类的完整名字显示 //xstream.alias("userinfo", userinfo.class); //xstream.alias("info", info.class); //xstream.alias("prodchinfo", prodchinfo.class); //xstream.alias("service", service.class); //xstream.alias("resource", resource.class); //xstream.alias("propara", propara.class); xs.toXML(e, fs); } catch (FileNotFoundException e1) { e1.printStackTrace(); } } } |
清单 3. Reader.java 反序列化对象
package com.samples; import java.io.FileInputStream; import java.io.FileNotFoundException; import com.thoughtworks.xstream.*; import com.thoughtworks.xstream.io.xml.DomDriver; public class Reader { public static void main(String[] args) { XStream xs = new XStream(new DomDriver()); Employee e = new Employee(); try { FileInputStream fis = new FileInputStream("c:/temp/employeedata.txt"); //注意别名的运用,此处使用别名,是根据清单二的使用别名对应,如果不使用,会找不到class错误。 //xstream.alias("userinfo", userinfo.class); //xstream.alias("info", info.class); //xstream.alias("prodchinfo", prodchinfo.class); //xstream.alias("service", service.class); //xstream.alias("resource", resource.class); //xstream.alias("propara", propara.class);xs.fromXML(fis, e); //print the data from the object that has been read System.out.println(e.toString()); } catch (FileNotFoundException ex) { ex.printStackTrace(); } } } |
多数应用程序都要用到一些属性。这些属性可能是要连接的数据源的名称或者日志文件的位置。配置文件最适合存储这类数据,从而在修改这些属性的时候不需要重新编译应用程序,应用程序维护起来也更容易。下面的例子说明如何在应用程序中使用 XStream 从 XML 配置文件中读取配置属性。
XML 数据绑定通常需要从 XML 文件生成 Java 对象。使用 XStream 省略了生成对象这个步骤。只需要编写一个 Java 类将类的字段映射到希望读取的 XML 的元素。这个例子使用的配置文件如 清单 4 所示。
<?xml version="1.0" encoding="UTF-8"?> <config> <datasource-name>IRIS</datasource-name> <ipaddress>9.124.74.85</ipaddress> <logfilename>DailyLogApplication.log</logfilename> <appender>console</appender> </config> |
清单 5 显示的 ConfigReader
类读取该配置文件并将属性加载到 Java 对象中。
package com.samples; import java.io.FileInputStream; import java.io.FileNotFoundException; import com.thoughtworks.xstream.*; import com.thoughtworks.xstream.io.xml.DomDriver; public class ConfigReader { String datasourcename = null; String ipaddress = null; String logfilename = null; String appender = null; @Override public String toString() { // This method prints out the values stored in the member variables return "Datasource Name : "+datasourcename+ " \nIP Address : "+ipaddress+ " \nLogfilename : "+logfilename+ " \nAppender : "+appender; } /** * @param args * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException { XStream xs = new XStream(new DomDriver()); FileInputStream fis = new FileInputStream("c:/temp/Config.xml"); xs.aliasField("datasource-name", ConfigReader.class, "datasourcename"); xs.alias("config", ConfigReader.class); ConfigReader r = (ConfigReader)xs.fromXML(fis); System.out.println(r.toString()); } } |
创建和开发使用 XStream 的应用程序只需要简单的几个步骤。本文介绍如何使用 XStream 序列化/逆序列化 Java 对象和读取配置文件,通过 XStream 网站(教程链接参见 参考资料)可以进一步了解别名、注解和转换程序。别名和转换程序可以让您完全控制生成的 XML。
<!-- CMA ID: 306793 --><!-- Site ID: 10 --><!-- XSLT stylesheet used to transform this file: dw-article-6.0-beta.xsl -->