码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-03-07 19:01:55      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:tac   veh   load   ber   stack   tcl   arc   return   null   

实体类

@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‘?>\n";

            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>\n                    <LICENSEKEY>5</LICENSEKEY>\n                    <KKBH>5</KKBH>\n                    <JGSK>xxxx</JGSK>\n                    <CDBH>3</CDBH>\n                    <HPHM>XY0001</HPHM>\n                    <HPYS>2</HPYS>\n                    <CPTP>xxxxx</CPTP>\n                </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反序列化,绑定到实体上

标签:tac   veh   load   ber   stack   tcl   arc   return   null   

原文地址:https://www.cnblogs.com/passedbylove/p/12436061.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!