java XML字符串和bean实体类互转

pom引入依赖

<dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.13.1</version>
        </dependency>

实体类

package vo;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.Data;

import java.util.List;

@Data
@JacksonXmlRootElement(localName = "res")
public class PayInfoResVo {
    /**
     * 处理结果代码:200-成功
     */
    @JacksonXmlProperty()  //此注解的localName属性可以不填,默认为字段名称,如果字段首字母是大写的话,那就必须填写了
    private String resultCode;

    /**
     * 处理结果描述
     */
    @JacksonXmlProperty()  //此注解的localName属性可以不填,默认为字段名称,如果字段首字母是大写的话,那就必须填写了
    private String resultDesc;

    /**
     * 就诊纪录信息的集合
     */
    @JacksonXmlElementWrapper(localName = "payList")  //这个是循环的根节点
    @JacksonXmlProperty()  //此注解的localName属性可以不填,默认为字段名称,如果字段首字母是大写的话,那就必须填写了
    private List<PayListInfoVo> payListInfo;
}
package vo;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.Data;

/**
 * 就诊纪录信息
 */
@Data
@JacksonXmlRootElement()
public class PayListInfoVo {
    /**
     * 就诊流水号
     */
    @JacksonXmlProperty()
    private String clinicSeq;

    /**
     * 医院代码
     */
    @JacksonXmlProperty()
    private String hospitalId;

    /**
     * 接诊科室代码
     */
    @JacksonXmlProperty()
    private String deptId;
}

工具类

package util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;

/**
 * XML和Bean互转工具类
 *
 * @author DaenMax
 * 2022-4-20 11:11:15
 */
public class XmlToObjectUtil {
    /**
     * XML转Bean
     *
     * @param xml
     * @param beanType
     * @param <T>
     * @return
     */
    public static <T> T convertXmlToObject(String xml, Class<T> beanType) {
        if (null != xml && !"".equals(xml)) {
            try {
                return new XmlMapper().readValue(xml, beanType);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * Bean转XML
     *
     * @param object
     * @return
     */
    public static String convertObjectToXml(Object object) {
        if (object != null) {
            try {
                XmlMapper xmlMapper = new XmlMapper();
                //设置xml格式携带版本和编码信息<?xml version='1.0' encoding='UTF-8'?>
                xmlMapper.enable(ToXmlGenerator.Feature.WRITE_XML_DECLARATION);
                //字段值为null,自动忽略,不再序列化
                xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
                return xmlMapper.writeValueAsString(object);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

测试

import util.XmlToObjectUtil;
import vo.PayInfoResVo;
import vo.PayListInfoVo;

import java.util.ArrayList;
import java.util.List;

public class test {
    public static void main(String[] args) {
        make();
        analysis();
    }

    /**
     * 生成
     */
    public static void make() {
        PayInfoResVo payInfoResVo = new PayInfoResVo();
        payInfoResVo.setResultCode("200");
        payInfoResVo.setResultDesc("成功");
        List<PayListInfoVo> list = new ArrayList<>();

        PayListInfoVo payListInfoVo1 = new PayListInfoVo();
        payListInfoVo1.setClinicSeq("123456");
        payListInfoVo1.setHospitalId("阿萨德");
        payListInfoVo1.setDeptId("fff");
        list.add(payListInfoVo1);

        PayListInfoVo payListInfoVo2 = new PayListInfoVo();
        payListInfoVo2.setClinicSeq("98465");
        payListInfoVo2.setHospitalId("设定图");
        payListInfoVo2.setDeptId("bbb");
        list.add(payListInfoVo2);

        PayListInfoVo payListInfoVo3 = new PayListInfoVo();
        payListInfoVo3.setClinicSeq("4567854");
        payListInfoVo3.setHospitalId("换接口");
        payListInfoVo3.setDeptId("jjj");
        list.add(payListInfoVo3);
        payInfoResVo.setPayListInfo(list);
        String xml = XmlToObjectUtil.convertObjectToXml(payInfoResVo);
        System.out.println(xml);
    }

    /**
     * 解析
     */
    public static void analysis() {
        String xml = "<?xml version='1.0' encoding='UTF-8'?><res><resultCode>200</resultCode><resultDesc>成功</resultDesc><payList><payListInfo><clinicSeq>123456</clinicSeq><hospitalId>阿萨德</hospitalId><deptId>fff</deptId></payListInfo><payListInfo><clinicSeq>98465</clinicSeq><hospitalId>设定图</hospitalId><deptId>bbb</deptId></payListInfo><payListInfo><clinicSeq>4567854</clinicSeq><hospitalId>换接口</hospitalId><deptId>jjj</deptId></payListInfo></payList></res>\n";
        PayInfoResVo payInfoResVo = XmlToObjectUtil.convertXmlToObject(xml, PayInfoResVo.class);
        System.out.println(payInfoResVo);
    }

}

效果

例外

另外有java XML字符串和json字符串互转代码,地址:https://www.cnblogs.com/daen/p/16779870.html

posted @ 2022-05-09 16:58  DaenMax  阅读(1399)  评论(0编辑  收藏  举报