将Java对象序列化成XML格式,将XML反序列化还原为Java对象

版权声明:本文为博主原创文章,转载请加上原文链接,谢谢! https://blog.csdn.net/sxdtzhaoxinguo/article/details/81980228

摘要:将Java对象序列化成XML格式,将XML反序列化还原为Java对象。

一:创建Maven工程,引入xstream依赖:

<!--xstream-->
    <dependency>
      <groupId>com.thoughtworks.xstream</groupId>
      <artifactId>xstream</artifactId>
      <version>1.4.4</version>
    </dependency>

二:创建Java序列化为XML格式、XML反序列化为Java对象的类:

package cn.micai.base.io;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import java.io.IOException;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/8/23 10:48
 * @Description: 将Java对象序列化成XML格式,将XML反序列化还原为Java对象
 */
public class XmlSerializeDeserializeMain {

    /**
     * 将Java对象序列化成XML格式
     * @param employee
     * @return
     * @throws IOException
     */
    public static String serialize(Employee employee){
        // 将employee对象序列化为XML
        XStream xStream = new XStream(new DomDriver());
        // 设置employee类的别名
        xStream.alias("employee", Employee.class);
        String personXml = xStream.toXML(employee);
        return personXml;
    }

    /**
     * 将XML反序列化还原为Java对象
     * @param personXml
     * @return
     */
    public static Employee deserialize(String personXml) {
        // 将employee对象序列化为XML
        XStream xStream = new XStream(new DomDriver());
        Employee employee = (Employee) xStream.fromXML(personXml);
        return employee;
    }

    public static void main(String [] args) {

        Employee employee = new Employee();
        employee.setEmployeeId(1);
        employee.setEmployeeName("赵新国");
        employee.setDepartment("软件工程师");
        // 序列化
        String serialize = serialize(employee);
        System.out.println(serialize);
        // 反序列化
        Employee deserialize = deserialize(serialize);
        System.out.println(deserialize.toString());

    }

}

三:创建Employee实体对象

package cn.micai.base.io;

import java.io.Serializable;

/**
 * 描述:
 * <p>
 *
 *     transient使用小结
 *     1.一旦变量被transient修饰,变量将不再是对象持久化的一部分,该变量内容在序列化后无法获得访问。
 *     2.transient关键字只能修饰变量,而不能修饰方法和类。注意,本地变量是不能被transient关键字修饰的。变量如果是用户自定义类变量,则该类需要实现Serializable接口。
 *     3.被transient关键字修饰的变量不再能被序列化,一个静态变量不管是否被transient修饰,均不能被序列化。
 *
 * @author: 赵新国
 * @date: 2018/6/7 12:10
 */
public class Employee implements Serializable {

    private static final long serialVersionUID = 1L;
    private int employeeId;
    private String employeeName;
    /**
     * 使用transient关键字,表示该字段不序列化
     */
    private transient String department;

    public int getEmployeeId() {
        return employeeId;
    }
    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }
    public String getEmployeeName() {
        return employeeName;
    }
    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "employeeId=" + employeeId +
                ", employeeName='" + employeeName + '\'' +
                ", department='" + department + '\'' +
                '}';
    }
}

四:运行测试:

五:异常提醒:

上面的异常信息说明了,在解析XML的时候,找不到对应的实体对象,所以会报上面的错误,那我们就在解析的时候,告诉xStream,是解析那个对象,修改解析XML代码如下:

/**
     * 将XML反序列化还原为Java对象
     * @param personXml
     * @return
     */
    public static Employee deserialize(String personXml) {
        // 将employee对象序列化为XML
        XStream xStream = new XStream(new DomDriver());
        xStream.alias("employee", Employee.class);
        Employee employee = (Employee) xStream.fromXML(personXml);
        return employee;
    }

六:最终运行结果:

 

展开阅读全文

没有更多推荐了,返回首页