序列化反序列化--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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值