java xml格式String与Objcet互转

该文章提供了一个Java工具类,用于实现XML与对象之间的转换,利用JAXBContext和Marshaller、Unmarshaller接口。转换过程中需要配合@XmlRootElement等注解来定义对象结构。类中包含了toXml方法将对象转换成XML字符串,以及fromXml方法将XML字符串反序列化为指定类型的对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

互转工具类



import lombok.extern.slf4j.Slf4j;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;

/**
 * Xml与对象转换工具类
 *
 * @author 
 * @date 2022/10/24
 */
@Slf4j
public class XmlUtil {
    public static String toXml(Object obj) {
        String xmlStr = null;
        JAXBContext jaxbContext = null;
        Marshaller marshaller = null;
        StringWriter writer = null;
        try {
            jaxbContext = JAXBContext.newInstance(obj.getClass());
            marshaller = jaxbContext.createMarshaller();
            writer = new StringWriter();
            marshaller.marshal(obj, writer);
            xmlStr = writer.toString();
        } catch (JAXBException e) {
            log.error("xml转对象失败");
        }
        return xmlStr;
    }

    public static <T> T fromXml(String xmlStr, Class<T> aClass) {
        T result = null;
        JAXBContext jaxbContext = null;
        try {
            StringReader reader = new StringReader(xmlStr);
            jaxbContext = JAXBContext.newInstance(aClass);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            result = (T) jaxbUnmarshaller.unmarshal(reader);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return result;
    }
}

类需要的注解 @XmlRootElement() @XmlAttribute() @XmlElement()


import lombok.Data;

import javax.xml.bind.annotation.*;

/**
 * 
 *
 * @author 
 * @date 2022/10/21
 */
@Data
@XmlRootElement(name = "VariCfgSec")
@XmlAccessorType(value = XmlAccessType.FIELD)
public class WebApiConfigReq {
    /**
     * action: DEVICE_QUERY查询设备版本相关信息
     */
    @XmlAttribute(name = "Action")
    private String action;
    private StSnap stSnap;
    private StOSD stOSD;
    @XmlElement(name = "stVersion")
    private String stVersion;

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement
    @Data
    public static class StOSD {
       
        @XmlAttribute(name = "ucFD_TempEnable")
        private String ucFD_TempEnable;
     
        @XmlAttribute(name = "ucFD_GPSEnable")
        private String ucFD_GPSEnable;
     
        @XmlAttribute(name = "ucFD_DeviceNoEnable")
        private String ucFD_DeviceNoEnable;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement
    @Data
    public static class StSnap {

        @XmlAttribute(name = "ucFD_MultiSnapEnable")
        private String ucMultiSnapEnable;

        @XmlAttribute(name = "uiFD_Interval")
        private String uiInterval;

        @XmlAttribute(name = "ucFD_SnapSubResolution")
        private String ucSnapSubResolution;
 
        @XmlAttribute(name = "ucFD_PrePSnapEn")
        private String ucPrePointSnapEn;

        @XmlAttribute(name = "ucFD_PrePSnapChn")
        private String ucPrePointSnapChn;
       
        @XmlAttribute(name = "strFD_PrePName")
        private String strPrePointName;
        
        @XmlAttribute(name = "strFD_PrePWSec")
        private String strPrePointWaitSec;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值