序列化反序列化--Xstream的使用

之前讲了fastjson的使用--将JavaBean与json对象之间互相转换。

该篇文章,教大家使用Xstream来实现XMl与JavaBean的转换。

第一步:

通过maven引入XStream的jar包。

  <!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.3.1</version>
</dependency>

第二步:

准备一个需要序列化和反序列化的类:

注:

1.该类必须有无参的构造函数,否则报错:Cannot construct com.claire.jing.test0811.Student as it does not have a no-args constructor

2.该类 implements Serializable ,这个接口仅仅是标识作用,写上只是标识该类是可以被序列化和反序列化的。

public class Student implements Serializable {
    
    private int id;
    private String name;
    private String school;
    
    public Student() {
        // TODO Auto-generated constructor stub
    }
    public Student(int id, String name, String school) {
        super();
        this.id = id;
        this.name = name;
        this.school = school;
    }
    
    
    
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSchool() {
        return school;
    }
    public void setSchool(String school) {
        this.school = school;
    }
    
    
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", school=" + school + "]";
    }
    
    

}

第三步:使用XStream来实现该类的序列化和反序列

public class SerializeXml {
    public static void main(String[] args) {
        serializeToXml();
     System.out.println("**************************************"); deserialize(); }
public static void serializeToXml() { FileOutputStream write = null; Student stu1 = new Student(10003, "claire", "hh"); Student stu2 = new Student(10004, "leafly", "hh"); Student[] students = {stu1,stu2}; XStream xstream = new XStream(); try {
        //将文流怼到文件上 write
= new FileOutputStream("F:/myObjtoXml.txt");
        //将学生对象序列化成Xml并通过流写入到文件中 xstream.toXML(students, write); }
catch (FileNotFoundException e) { e.printStackTrace(); } //将序列化结果输出 System.out.println(xstream.toXML(students)); } public static void deserialize() { FileInputStream reader; XStream xstream = new XStream(); Student[] students=null; try { reader = new FileInputStream("F:/myObjtoXml.txt");
        //将Xml文件反序列化为Student对象 students
=(Student[])xstream.fromXML(reader); if (students != null) { for (Student student : students) { System.out.println(student); } } } catch (FileNotFoundException e) { e.printStackTrace(); } } }

上面程序的输出结果为:

<com.claire.jing.test0811.Student-array>
<com.claire.jing.test0811.Student>
<id>10003</id>
<name>claire</name>
<school>hh</school>
</com.claire.jing.test0811.Student>
<com.claire.jing.test0811.Student>
<id>10004</id>
<name>leafly</name>
<school>hh</school>
</com.claire.jing.test0811.Student>
</com.claire.jing.test0811.Student-array>
**************************************
Student [id=10003, name=claire, school=hh]
Student [id=10004, name=leafly, school=hh]

上面都是最简单的序列化和反序列化的格式:

1.当Xml中有属性的时候,如:

<student age="15">
使用上面的反序列化就会报错,此时只要加上:
xstream.useAttributeFor(Student.class, "age");
 
2.序列化出来的xml文件中,都带有包名,如果不想让其显示类全名,只要给该类起个别名即可:
xstream.alias("student", Student.class);
原文地址:https://www.cnblogs.com/clairejing/p/9459051.html