欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

字符串类型的xml数据,反序列化为自定义的实体类

程序员文章站 2022-06-16 12:51:14
...

应用场景

lz最近在开发一个接口,需要接收xml类型的数据,然后对接收到的数据进行业务处理。
关于如何从请求中接收数据,CSDN上有很多文章。
再次只介绍下,我是如何把接收到的xml字符串转化为实体类的,大家有不同见解,欢迎给予建议。

准备以及实现

1.xml数据
批次(一个请求中可以存在多个批次)batch
批次编号batchCode
订单(一个批次号中可以存在多个订单)soOrder
订单编码orderCode
打印顺序printOrder

<?xml version="1.0" encoding="utf-8"?>
<request>
<batchs>
<batch>
<batchCode>22</batchCode>
<soOrders>
<soOrder>
<orderCode>80000002866</orderCode>
<printOrder>10</printOrder>
</soOrder>
<soOrder>
<orderCode>08011801407</orderCode>
<printOrder>11</printOrder>
</soOrder>
</soOrders>
</batch>
<batch>
<batchCode>33</batchCode>
<soOrders>
<soOrder>
<orderCode>08011801435</orderCode>
<printOrder>10</printOrder>
</soOrder>
<soOrder>
<orderCode>08011801445</orderCode>
<printOrder>11</printOrder>
</soOrder>
</soOrders>
</batch>
</batchs>
</request>

2.序列化与反序列化工具类XmlUtils

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 * Copyright (c) 2017. ****** All Rights Reserved.
 * 
 * @描述: TODO
 * @作者: lizy
 * @创建: 2018-2-7 下午3:56:49
 * @版本: V1.0 *
 */
public class XmlUtils {
    /**
     * 将对象直接转换成String类型的 XML输出
     * 
     * @param obj
     * @return
     */
    public static String convertToXml(Object obj) {
        // 创建输出流
        StringWriter sw = new StringWriter();
        try {
            // 利用jdk中自带的转换类实现
            JAXBContext context = JAXBContext.newInstance(obj.getClass());

            Marshaller marshaller = context.createMarshaller();
            // 格式化xml输出的格式
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                    Boolean.TRUE);
            // 将对象转换成输出流形式的xml
            marshaller.marshal(obj, sw);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return sw.toString();
    }

    /**
     * 将String类型的xml转换成对象
     */
    public static Object convertXmlStrToObject(Class clazz, String xmlStr) {
        Object xmlObject = null;
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            // 进行将Xml转成对象的核心接口
            Unmarshaller unmarshaller = context.createUnmarshaller();
            StringReader sr = new StringReader(xmlStr);
            xmlObject = unmarshaller.unmarshal(sr);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return xmlObject;
    }
}

3.相关实体类
请求类ThermOrderPrintRequest

import java.io.Serializable;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * Copyright (c) 2017. ****** All Rights Reserved.
 * 
 * @描述: 
 * @作者: lizy
 * @创建: 2018-2-7 下午4:24:51
 * @版本: V1.0 *
 */
@XmlRootElement(name = "request")
@XmlAccessorType(XmlAccessType.FIELD)
public class ThermOrderPrintRequest implements Serializable{

    @XmlElementWrapper(name = "batchs")
    @XmlElement(name = "batch")
    private List<ThermOrderPrintBatch> batchs;

    public List<ThermOrderPrintBatch> getBatchs() {
        return batchs;
    }

    public void setBatchs(List<ThermOrderPrintBatch> batchs) {
        this.batchs = batchs;
    }

}

请求批次类ThermOrderPrintBatch

import java.io.Serializable;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlList;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * Copyright (c) 2017. ***** All Rights Reserved.
 * 
 * @描述: TODO
 * @作者: lizy
 * @创建: 2018-2-7 下午4:17:10
 * @版本: V1.0 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
public class ThermOrderPrintBatch implements Serializable{

    @XmlElement(name = "batchCode")
    private String batchCode;// 批次编号

    @XmlElementWrapper(name = "soOrders")
    @XmlElement(name = "soOrder")
    private List<ThermOrderPrint> orderPrints;

    public String getBatchCode() {
        return batchCode;
    }

    public void setBatchCode(String batchCode) {
        this.batchCode = batchCode;
    }

    public List<ThermOrderPrint> getOrderPrints() {
        return orderPrints;
    }

    public void setOrderPrints(List<ThermOrderPrint> orderPrints) {
        this.orderPrints = orderPrints;
    }

}

订单类ThermOrderPrint

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * Copyright (c) 2017. ****** All Rights Reserved.
 * 
 * @描述: 
 * @作者: lizy
 * @创建: 2018-2-7 下午4:00:53
 * @版本: V1.0 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
public class ThermOrderPrint implements Serializable{

    @XmlElement(name = "orderCode")
    private String orderCode;// 订单编码

    @XmlElement(name = "printOrder")
    private String printOrder;// 打印顺序

    public String getOrderCode() {
        return orderCode;
    }
    public void setOrderCode(String orderCode) {
        this.orderCode = orderCode;
    }
    public String getPrintOrder() {
        return printOrder;
    }
    public void setPrintOrder(String printOrder) {
        this.printOrder = printOrder;
    }

}

4.反序列化为ThermOrderPrintRequest

ThermOrderPrintRequest orderPrintRequest = (ThermOrderPrintRequest) XmlUtils
                            .convertXmlStrToObject(
                                    ThermOrderPrintRequest.class,XmlString);

注意事项

  1. xml内容不能有转义字符,否则在反序列化会报错;
  2. @XmlRootElement在请求类加注释,表示为根节点;
  3. @XmlAccessorType用了这个之后就会自动忽略setter/getter方法。不用这个就需要去掉注解,需要保证属性名和xml里的表签名一致;
  4. @XmlElement(name = "batch")表明节点,name需要和节点名一致;
  5. @XmlElementWrapper(name = "batchs")表示生成一个包装 XML 表示形式的包装器元素。 此元素主要用于生成一个包装集合的包装器 XML 元素;
相关标签: xml 反序列化