java中xml反序列化,绑定到实体上

Posted 学习笔记

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中xml反序列化,绑定到实体上相关的知识,希望对你有一定的参考价值。

实体类

@XStreamAlias("ROOT")
public class VehiclePassInfo {
    @Id
    @GeneratedValue(generator = "JDBC")
    private Integer id;
    @XStreamAlias("LICENSEKEY")
    private String licensekey;
    @XStreamAlias("KKBH")
    private String interceptId;
    @XStreamAlias("JGSK")
    private String passTime;
    @XStreamAlias("CDBH")
    private String laneId;
    @XStreamAlias("HPHM")
    private String licensePlateNumber;
    @XStreamAlias("HPYS")
    private String licensePlateColour;
    @XStreamAlias("CPTP")
    private String licensePlatePic;

    public void setId(Integer id) {
        this.id = id;
    }

    public void setLicensekey(String licensekey) {
        this.licensekey = licensekey;
    }

    public void setInterceptId(String interceptId) {
        this.interceptId = interceptId;
    }

    public void setPassTime(String passTime) {
        this.passTime = passTime;
    }

    public void setLaneId(String laneId) {
        this.laneId = laneId;
    }

    public void setLicensePlateNumber(String licensePlateNumber) {
        this.licensePlateNumber = licensePlateNumber;
    }

    public void setLicensePlateColour(String licensePlateColour) {
        this.licensePlateColour = licensePlateColour;
    }

    public void setLicensePlatePic(String licensePlatePic) {
        this.licensePlatePic = licensePlatePic;
    }
   

    public String toString() {
        return "VehiclePassInfo(id=" + getId() + ", licensekey=" +
        getLicensekey() + ", interceptId=" + getInterceptId() + ", passTime=" +
        getPassTime() + ", laneId=" + getLaneId() + ", licensePlateNumber=" +
        getLicensePlateNumber() + ", licensePlateColour=" +
        getLicensePlateColour() + ", licensePlatePic=" + getLicensePlatePic() +
        ")";
    }

    public Integer getId() {
        return this.id;
    }

    public String getLicensekey() {
        return this.licensekey;
    }

    public String getInterceptId() {
        return this.interceptId;
    }

    public String getPassTime() {
        return this.passTime;
    }

    public String getLaneId() {
        return this.laneId;
    }

    public String getLicensePlateNumber() {
        return this.licensePlateNumber;
    }

    public String getLicensePlateColour() {
        return this.licensePlateColour;
    }

    public String getLicensePlatePic() {
        return this.licensePlatePic;
    }
}
import com.xxx.VehiclePassInfo;
import com.netmarch.dataacces.util.XmlUtil;

import com.thoughtworks.xstream.XStream;

import javax.validation.constraints.NotNull;


public class XmlUtil {
    public static XStream xStreamTo = new XStream();
    public static XStream xStreamFrom = new XStream();

    static {
        xStreamTo.autodetectAnnotations(true);
    }

    public static String toXml(@NotNull
    Object bean) {
        try {
            String top = "<?xml version=‘1.0‘ encoding=‘UTF-8‘?>
";

            return top + xStreamTo.toXML(bean);
        } catch (Exception e) {
            e.printStackTrace();

            return null;
        }
    }

    public static <T> T fromXml(@NotNull
    String xml, @NotNull
    Class<T> clzz) {
        try {
            xStreamFrom.processAnnotations(clzz);
            xStreamFrom.setClassLoader(clzz.getClassLoader());
            xStreamFrom.allowTypes(new Class[] { clzz });

            return (T) xStreamFrom.fromXML(xml);
        } catch (Exception e) {
            e.printStackTrace();

            return null;
        }
    }

    public static void main(String[] args) {
        VehiclePassInfo vehiclePassInfo = (VehiclePassInfo) fromXml("<ROOT>
                    <LICENSEKEY>5</LICENSEKEY>
                    <KKBH>5</KKBH>
                    <JGSK>xxxx</JGSK>
                    <CDBH>3</CDBH>
                    <HPHM>XY0001</HPHM>
                    <HPYS>2</HPYS>
                    <CPTP>xxxxx</CPTP>
                </ROOT>",
                VehiclePassInfo.class);

        System.out.println(vehiclePassInfo.toString());
    }
}

 

maven依赖:

<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.9</version>
</dependency>

 

以上是关于java中xml反序列化,绑定到实体上的主要内容,如果未能解决你的问题,请参考以下文章

delphi 对象序列化保存到文件中,反序列化后出现乱码

xml序列化和反序列化

XML文件与实体类的互相转换

XML和实体类之间相互转换(序列化和反序列化)

XML和实体类之间相互转换(序列化和反序列化)

C# XML和实体类之间相互转换(序列化和反序列化)