1.Xstream介绍

(1)Xstream介绍
    Xstream是一种OXMapping 技术,是用来处理XML文件序列化的框架,在将JavaBean序列化,或将XML文件反序列化的时候,不需要其它辅助类和映射文件,使得XML序列化不再繁索。Xstream也可以将JavaBean序列化成Json或反序列化,使用非常方便。
(2)Xstream的简单例子
  1. class Person//JavaBean实体类
  2. {
  3. private String name;
  4. private int age;
  5. public Person(String name,int age)
  6. {
  7. this.name=name;
  8. this.age=age;
  9. }
  10. @Override
  11. public String toString()
  12. {
  13. return "Person [name=" + name + ", age=" + age + "]";
  14. }
  15. }
  16. public class Test
  17. {
  18. public static void main(String[] args)
  19. {
  20. Person bean=new Person("张三",19);
  21. XStream xstream = new XStream();
  22. //XML序列化
  23. String xml = xstream.toXML(bean);
  24. System.out.println(xml);
  25. //XML反序列化
  26. bean=(Person)xstream.fromXML(xml);
  27. System.out.println(bean);
  28. xstream = new XStream(new JettisonMappedXmlDriver());
  29. xstream.setMode(XStream.NO_REFERENCES);
  30. //Json序列化
  31. String json=xstream.toXML(bean);
  32. System.out.println(json);
  33. //Json反序列
  34. bean=(Person)xstream.fromXML(json);
  35. System.out.println(bean);
  36. }
  37. }
    程序运行结果:
  1. <test.Person>
  2. <name>张三</name>
  3. <age>19</age>
  4. </test.Person>
  5. Person [name=张三, age=19]
  6. {"test.Person":{"name":"张三","age":19}}
  7. Person [name=张三, age=19
    注意:Xstream序列化XML时需要引用的jar包:xstream-[version].jar、xpp3-[version].jar、xmlpull-[version].jar。Xstream序列化Json需要引用的jar包:jettison-[version].jar。
    使用Xstream序列化时,对JavaBean没有任何限制。JavaBean的字段可以是私有的,也可以没有getter或setter方法,还可以没有默认的构造函数。

2.Xstream的基本使用

(1)Xstream序列化XML
    Xstream序列化XML时可以允许用户使用不同的XML解析器,用户可以使用一个标准的JAXP DOM解析器或自Java6集成StAX解析器。这样用户就不需要依赖xpp3-[version].jar。
    Xstream序列化XML时,也可以对XML节点重命名。
  1. public class Test
  2. {
  3. public static void main(String[] args)
  4. {
  5. Person bean=new Person("张三",19);
  6. //XStream xstream = new XStream();//需要XPP3库
  7. //XStream xstream = new XStream(new DomDriver());//不需要XPP3库
  8. XStream xstream = new XStream(new StaxDriver());//不需要XPP3库开始使用Java6
  9. xstream.alias("人",Person.class);//为类名节点重命名
  10. //XML序列化
  11. String xml = xstream.toXML(bean);
  12. System.out.println(xml);
  13. //XML反序列化
  14. bean=(Person)xstream.fromXML(xml);
  15. System.out.println(bean);
  16. }
  17. }
    程序运行结果:
  1. <?xml version="1.0" ?><人><name>张三</name><age>19</age></人>
  2. Person [name=张三, age=19]
(2)Xstream序列化Json
    Xstream序列化Json与序列化XML类似,例如:
  1. public class Test
  2. {
  3. public static void main(String[] args)
  4. {
  5. Person bean=new Person("张三",19);
  6. XStream xstream = new XStream(new JettisonMappedXmlDriver());//设置Json解析器
  7. xstream.setMode(XStream.NO_REFERENCES);//设置reference模型,不引用
  8. xstream.alias("人",Person.class);//为类名节点重命名
  9. //Json序列化
  10. String xml = xstream.toXML(bean);
  11. System.out.println(xml);
  12. //Json反序列化
  13. bean=(Person)xstream.fromXML(xml);
  14. System.out.println(bean);
  15. }
  16. }
    程序运行结果:
  1. {"人":{"name":"张三","age":19}}
  2. Person [name=张三, age=19]

3.Xstream序列化重命名

(1)为包重命名:Xstream.aliasPackage()方法
  1. public class Test
  2. {
  3. public static void main(String[] args)
  4. {
  5. Person bean=new Person("张三",19);
  6. XStream xstream = new XStream();
  7. xstream.aliasPackage("com.lzw", "test");//为包名称重命名
  8. //序列化
  9. String xml = xstream.toXML(bean);
  10. System.out.println(xml);
  11. //反序列化
  12. bean=(Person)xstream.fromXML(xml);
  13. System.out.println(bean);
  14. }
  15. }
    程序运行结果:
  1. <com.lzw.Person>
  2. <name>张三</name>
  3. <age>19</age>
  4. </com.lzw.Person>
  5. Person [name=张三, age=19]
(2)为类重命名:Xstream.alias()方法
  1. public class Test
  2. {
  3. public static void main(String[] args)
  4. {
  5. Person bean=new Person("张三",19);
  6. XStream xstream = new XStream();
  7. xstream.alias("人", Person.class);//为类名节点重命名
  8. //序列化
  9. String xml = xstream.toXML(bean);
  10. System.out.println(xml);
  11. //反序列化
  12. bean=(Person)xstream.fromXML(xml);
  13. System.out.println(bean);
  14. }
  15. }
    程序运行结果:
  1. <人>
  2. <name>张三</name>
  3. <age>19</age>
  4. </人>
  5. Person [name=张三, age=19]
(3)为字段重命名:Xstream.aliasField()方法
  1. public class Test
  2. {
  3. public static void main(String[] args)
  4. {
  5. Person bean=new Person("张三",19);
  6. XStream xstream = new XStream();
  7. xstream.aliasField("姓名", Person.class,"name");//为类的字段节点重命名
  8. xstream.aliasField("年龄", Person.class,"age");//为类的字段节点重命名
  9. //序列化
  10. String xml = xstream.toXML(bean);
  11. System.out.println(xml);
  12. //反序列化
  13. bean=(Person)xstream.fromXML(xml);
  14. System.out.println(bean);
  15. }
  16. }
    程序运行结果:
  1. <test.Person>
  2. <姓名>张三</姓名>
  3. <年龄>19</年龄>
  4. </test.Person>
  5. Person [name=张三, age=19]
(4)省略集合根节点:Xstream.addImplicitCollection()方法
  1. class Person
  2. {
  3. private String name;
  4. private int age;
  5. private List friends;
  6. public Person(String name, int age, String... friends)
  7. {
  8. this.name = name;
  9. this.age = age;
  10. this.friends = Arrays.asList(friends);
  11. }
  12. @Override
  13. public String toString()
  14. {
  15. return "Person [name=" + name + ", age=" + age + ", friends=" + friends + "]";
  16. }
  17. }
  18. public class Test
  19. {
  20. public static void main(String[] args)
  21. {
  22. Person bean =new Person("张三",19,"李四","王五","赵六");
  23. XStream xstream = new XStream();
  24. xstream.addImplicitCollection(Person.class, "friends");//省略集合根节点
  25. //序列化
  26. String xml = xstream.toXML(bean);
  27. System.out.println(xml);
  28. //反序列化
  29. bean=(Person)xstream.fromXML(xml);
  30. System.out.println(bean);
  31. }
  32. }
    程序运行结果:
  1. <test.Person>
  2. <name>张三</name>
  3. <age>19</age>
  4. <string>李四</string>
  5. <string>王五</string>
  6. <string>赵六</string>
  7. </test.Person>
  8. Person [name=张三, age=19, friends=[李四, 王五, 赵六]]
(5)把字段节点设置成属性:Xstream.useAttributeFor()方法
  1. public class Test
  2. {
  3. public static void main(String[] args)
  4. {
  5. Person bean =new Person("张三",19,"李四","王五","赵六");
  6. XStream xstream = new XStream();
  7. xstream.useAttributeFor(Person.class, "name");//把字段节点设置成属性
  8. //序列化
  9. String xml = xstream.toXML(bean);
  10. System.out.println(xml);
  11. //反序列化
  12. bean=(Person)xstream.fromXML(xml);
  13. System.out.println(bean);
  14. }
  15. }
    程序运行结果:
  1. <test.Person name="张三">
  2. <age>19</age>
  3. <friends class="java.util.Arrays$ArrayList">
  4. <a class="string-array">
  5. <string>李四</string>
  6. <string>王五</string>
  7. <string>赵六</string>
  8. </a>
  9. </friends>
  10. </test.Person>
  11. Person [name=张三, age=19, friends=[李四, 王五, 赵六]]
(6)隐藏字段:xstream.omitField()方法
  1. public class Test
  2. {
  3. public static void main(String[] args)
  4. {
  5. Person bean =new Person("张三",19,"李四","王五","赵六");
  6. XStream xstream = new XStream();
  7. xstream.omitField(Person.class, "friends");//把字段节点隐藏
  8. //序列化
  9. String xml = xstream.toXML(bean);
  10. System.out.println(xml);
  11. //反序列化
  12. bean=(Person)xstream.fromXML(xml);
  13. System.out.println(bean);
  14. }
  15. }
    程序运行结果:
  1. <test.Person>
  2. <name>张三</name>
  3. <age>19</age>
  4. </test.Person>
  5. Person [name=张三, age=19, friends=null]

4.Xstream注解的使用

(1)设置Xstream应用注解
    使用Xstream注解前需要对Xstream进行配置,可以使用两种方式:应用某个JavaBean类的注解或自动使用JavaBean类的注解。代码如下:
  1. XStream xstream = new XStream();
  2. xstream.processAnnotations(Person.class);//应用Person类的注解
  3. xstream.autodetectAnnotations(true);//自动检测注解
(2)重命名注解:@XStreamAlias()
  1. @XStreamAlias("人")
  2. class Person
  3. {
  4. @XStreamAlias("姓名")
  5. private String name;
  6. @XStreamAlias("年龄")
  7. private int age;
  8. @XStreamAlias("朋友")
  9. private List friends;
  10. public Person(String name, int age, String... friends)
  11. {
  12. this.name = name;
  13. this.age = age;
  14. this.friends = Arrays.asList(friends);
  15. }
  16. @Override
  17. public String toString()
  18. {
  19. return "Person [name=" + name + ", age=" + age + ", friends=" + friends + "]";
  20. }
  21. }
    程序运行结果:
  1. <人>
  2. <姓名>张三</姓名>
  3. <年龄>19</年龄>
  4. <朋友 class="java.util.Arrays$ArrayList">
  5. <a class="string-array">
  6. <string>李四</string>
  7. <string>王五</string>
  8. <string>赵六</string>
  9. </a>
  10. </朋友>
  11. </人>
  12. Person [name=张三, age=19, friends=[李四, 王五, 赵六]]
(3)省略集合根节点:@XStreamImplicit
  1. class Person
  2. {
  3. private String name;
  4. private int age;
  5. //@XStreamImplicit//只隐藏集合根节点
  6. @XStreamImplicit(itemFieldName="朋友")//设置重复的节点名,可能会导致无法反序列化
  7. private List<String> friends;
  8. public Person(String name, int age, String... friends)
  9. {
  10. this.name = name;
  11. this.age = age;
  12. this.friends = Arrays.asList(friends);
  13. }
  14. @Override
  15. public String toString()
  16. {
  17. return "Person [name=" + name + ", age=" + age + ", friends=" + friends + "]";
  18. }
  19. }
    程序运行结果:
  1. <test.Person>
  2. <name>张三</name>
  3. <age>19</age>
  4. <朋友>李四</朋友>
  5. <朋友>王五</朋友>
  6. <朋友>赵六</朋友>
  7. </test.Person>
  8. Person [name=张三, age=19, friends=[李四, 王五, 赵六]]
(4)把字段节点设置成属性:@XStreamAsAttribute
  1. class Person
  2. {
  3. @XStreamAsAttribute
  4. private String name;
  5. @XStreamAsAttribute
  6. private int age;
  7. private List<String> friends;
  8. public Person(String name, int age, String... friends)
  9. {
  10. this.name = name;
  11. this.age = age;
  12. this.friends = Arrays.asList(friends);
  13. }
  14. @Override
  15. public String toString()
  16. {
  17. return "Person [name=" + name + ", age=" + age + ", friends=" + friends + "]";
  18. }
  19. }
    程序运行结果:
  1. <test.Person name="张三" age="19">
  2. <friends class="java.util.Arrays$ArrayList">
  3. <a class="string-array">
  4. <string>李四</string>
  5. <string>王五</string>
  6. <string>赵六</string>
  7. </a>
  8. </friends>
  9. </test.Person>
  10. Person [name=张三, age=19, friends=[李四, 王五, 赵六]]
(5)隐藏字段:@XStreamOmitField
  1. class Person
  2. {
  3. private String name;
  4. private int age;
  5. @XStreamOmitField
  6. private List<String> friends;
  7. public Person(String name, int age, String... friends)
  8. {
  9. this.name = name;
  10. this.age = age;
  11. this.friends = Arrays.asList(friends);
  12. }
  13. @Override
  14. public String toString()
  15. {
  16. return "Person [name=" + name + ", age=" + age + ", friends=" + friends + "]";
  17. }
  18. }
    程序运行结果:
  1. <test.Person>
  2. <name>张三</name>
  3. <age>19</age>
  4. </test.Person>
  5. Person [name=张三, age=19, friends=null]
(6)设置转换器:@XStreamConverter()
  1. class Person
  2. {
  3. private String name;
  4. private int age;
  5. @XStreamConverter(value=BooleanConverter.class,booleans={false},strings={"男","女"})
  6. private boolean sex;
  7. public Person(String name, int age, boolean sex)
  8. {
  9. this.name = name;
  10. this.age = age;
  11. this.sex=sex;
  12. }
  13. @Override
  14. public String toString()
  15. {
  16. return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
  17. }
  18. }
    程序运行结果:
  1. <test.Person>
  2. <name>张三</name>
  3. <age>19</age>
  4. <sex>男</sex>
  5. </test.Person>
  6. Person [name=张三, age=19, sex=true]

5.Xstream自定义的转换器

(1)Xstream自带的转换器
    Xstream内部有许多转换器,用于JavaBean对象到XML或Json之间的转换。这些转换器的详细信息网址:http://xstream.codehaus.org/converters.html
(2)使用自定义的转换器
  1. class Person
  2. {
  3. private String name;
  4. private int age;
  5. public Person(String name, int age)
  6. {
  7. this.name = name;
  8. this.age = age;
  9. }
  10. public String getName()
  11. {
  12. return name;
  13. }
  14. public void setName(String name)
  15. {
  16. this.name = name;
  17. }
  18. public int getAge()
  19. {
  20. return age;
  21. }
  22. public void setAge(int age)
  23. {
  24. this.age = age;
  25. }
  26. @Override
  27. public String toString()
  28. {
  29. return "Person [name=" + name + ", age=" + age + "]";
  30. }
  31. }
  32. public class PersonConverter implements Converter
  33. {
  34. @Override//定义转换器能转换的JavaBean类型
  35. public boolean canConvert(Class type)
  36. {
  37. return type.equals(Person.class);
  38. }
  39. @Override//把对象序列化成XML或Json
  40. public void marshal(Object value, HierarchicalStreamWriter writer,
  41. MarshallingContext context)
  42. {
  43. Person person = (Person) value;
  44. writer.startNode("姓名");
  45. writer.setValue(person.getName());
  46. writer.endNode();
  47. writer.startNode("年龄");
  48. writer.setValue(person.getAge()+"");
  49. writer.endNode();
  50. writer.startNode("转换器");
  51. writer.setValue("自定义的转换器");
  52. writer.endNode();
  53. }
  54. @Override//把XML或Json反序列化成对象
  55. public Object unmarshal(HierarchicalStreamReader reader,
  56. UnmarshallingContext context)
  57. {
  58. Person person = new Person("",-1);
  59. reader.moveDown();
  60. person.setName(reader.getValue());
  61. reader.moveUp();
  62. reader.moveDown();
  63. person.setAge(Integer.parseInt(reader.getValue()));
  64. reader.moveUp();
  65. return person;
  66. }
  67. }
  68. public class Test
  69. {
  70. public static void main(String[] args)
  71. {
  72. Person bean =new Person("张三",19);
  73. XStream xstream = new XStream();
  74. xstream.registerConverter(new PersonConverter());//注册转换器
  75. //序列化
  76. String xml = xstream.toXML(bean);
  77. System.out.println(xml);
  78. //反序列化
  79. bean=(Person)xstream.fromXML(xml);
  80. System.out.println(bean);
  81. }
  82. }
    程序运行结果:
  1. <test.Person>
  2. <姓名>张三</姓名>
  3. <年龄>19</年龄>
  4. <转换器>自定义的转换器</转换器>
  5. </test.Person>
  6. Person [name=张三, age=19]
(3)常用的转换器接口与抽象类
    SingleValueConverter:单值转换接口
    AbstractSingleValueConverter:单值转换抽象类
    Converter:常规转换器接口

6.Xstream对象流的使用

(1)Xstream对象输出流
  1. class Person
  2. {
  3. private String name;
  4. private int age;
  5. public Person(String name, int age)
  6. {
  7. this.name = name;
  8. this.age = age;
  9. }
  10. }
  11. public class Test
  12. {
  13. public static void main(String[] args) throws IOException
  14. {
  15. XStream xstream = new XStream();
  16. ObjectOutputStream out = xstream.createObjectOutputStream(System.out);
  17. out.writeObject(new Person("张三",12));
  18. out.writeObject(new Person("李四",19));
  19. out.writeObject("Hello");
  20. out.writeInt(12345);
  21. out.close();
  22. }
  23. }
    程序运行结果:
  1. <object-stream>
  2. <test.Person>
  3. <name>张三</name>
  4. <age>12</age>
  5. </test.Person>
  6. <test.Person>
  7. <name>李四</name>
  8. <age>19</age>
  9. </test.Person>
  10. <string>Hello</string>
  11. <int>12345</int>
  12. </object-stream>
    注意:XStream对象流是通过标准java.io.ObjectOutputStream和java.io.ObjectInputStream对象。因为XML文档只能有一个根节点,必须包装在一个序列化的所有元素 额外的根节点。 这个根节点默认 < object-stream > 上面的例子所示。 
(2)Xstream对象输出流
  1. class Person
  2. {
  3. private String name;
  4. private int age;
  5. public Person(String name, int age)
  6. {
  7. this.name = name;
  8. this.age = age;
  9. }
  10. @Override
  11. public String toString()
  12. {
  13. return "Person [name=" + name + ", age=" + age + "]";
  14. }
  15. }
  16. public class Test
  17. {
  18. public static void main(String[] args) throws IOException, ClassNotFoundException
  19. {
  20. String s="<object-stream><test.Person><name>张三</name><age>12</age></test.Person><int>12345</int></object-stream>";
  21. StringReader reader = new StringReader(s);
  22. XStream xstream = new XStream();
  23. ObjectInputStream in = xstream.createObjectInputStream(reader);
  24. System.out.println((Person) in.readObject());
  25. System.out.println(in.readInt());
  26. }
  27. }
    程序运行结果:
  1. Person [name=张三, age=12]
  2. 12345

7.Xstream持久化API

(1)保存JavaBean对象
  1. class Person
  2. {
  3. private String name;
  4. private int age;
  5. public Person(String name, int age)
  6. {
  7. this.name = name;
  8. this.age = age;
  9. }
  10. @Override
  11. public String toString()
  12. {
  13. return "Person [name=" + name + ", age=" + age + "]";
  14. }
  15. }
  16. public class Test
  17. {
  18. public static void main(String[] args) throws IOException, ClassNotFoundException
  19. {
  20. PersistenceStrategy strategy = new FilePersistenceStrategy(new File("D:\\tmp"));
  21. List list = new XmlArrayList(strategy);
  22. list.add(new Person("张三",13));//保存数据
  23. list.add(new Person("李四",21));
  24. list.add(new Person("王五",17));
  25. }
  26. }
    程序运行结果:如果我们检查D:\tmp目录,有三个文件:int@0.xml、int@1.xml、int@2.xml;每个对象都被序列化到XML文件里。
(2)读取并删除JavaBean对象
  1. public class Test
  2. {
  3. public static void main(String[] args) throws IOException, ClassNotFoundException
  4. {
  5. PersistenceStrategy strategy = new FilePersistenceStrategy(new File("D:\\tmp"));
  6. List list = new XmlArrayList(strategy);
  7. for (Iterator it = list.iterator(); it.hasNext();)
  8. {
  9. System.out.println((Person) it.next());
  10. it.remove();//删除对象序列化文件
  11. }
  12. }
  13. }
    程序运行结果:
  1. Person [name=张三, age=13]
  2. Person [name=李四, age=21]
  3. Person [name=王五, age=17]

8.Xstream操作Json

(1)Xstream序列化Json的重命名
  1. @XStreamAlias("人")
  2. class Person
  3. {
  4. @XStreamAlias("姓名")
  5. private String name;
  6. @XStreamAlias("年龄")
  7. private int age;
  8. public Person(String name, int age)
  9. {
  10. this.name = name;
  11. this.age = age;
  12. }
  13. @Override
  14. public String toString()
  15. {
  16. return "Person [name=" + name + ", age=" + age + "]";
  17. }
  18. }
  19. public class Test
  20. {
  21. public static void main(String[] args)
  22. {
  23. Person bean=new Person("张三",19);
  24. XStream xstream = new XStream(new JettisonMappedXmlDriver());//设置Json解析器
  25. xstream.autodetectAnnotations(true);
  26. //Json序列化
  27. String xml = xstream.toXML(bean);
  28. System.out.println(xml);
  29. //Json反序列化
  30. bean=(Person)xstream.fromXML(xml);
  31. System.out.println(bean);
  32. }
  33. }
    程序运行结果:
  1. {"人":{"姓名":"张三","年龄":19}}
  2. Person [name=张三, age=19]
    注意:Xstream序列化Json的重命名的方式与其序列化成XML的方式一样!
(2)去掉序列化Json的根节点
  1. class Person
  2. {
  3. private String name;
  4. private int age;
  5. public Person(String name, int age)
  6. {
  7. this.name = name;
  8. this.age = age;
  9. }
  10. @Override
  11. public String toString()
  12. {
  13. return "Person [name=" + name + ", age=" + age + "]";
  14. }
  15. }
  16. public class Test00
  17. {
  18. public static void main(String[] args)
  19. {
  20. Person bean=new Person("张三",19);
  21. XStream xstream = new XStream(new JsonHierarchicalStreamDriver()
  22. {
  23. public HierarchicalStreamWriter createWriter(Writer writer)
  24. {
  25. return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE);
  26. }
  27. });
  28. //Json序列化
  29. String xml = xstream.toXML(bean);
  30. System.out.println(xml);
  31. }
  32. }
    程序运行结果:
  1. { "name": "张三", "age": 19 }
    注意:去掉根节点后的Json串是不能反序列化的,因为XStream 不知道它的类型。
(3)Json的解析器区别
    前面两个例子使用了不同的Json解析器,这里说明他们的不同之处:
    1. JettisonMappedXmlDriver:是支持序列化和反序列化Json的。
    2. JsonHierarchicalStreamDriver:只支持序列化,不支持反序列化。
-------------------------------------------------------------------------------------------------------------------------------




posted on 2015-03-04 15:33  技术江湖-小焕  阅读(37226)  评论(3编辑  收藏  举报