• 使用JAXB将Java对象转xml


    使用JAXB将Java对象转xml

    1. 要求生成的xml

    
    <root>
        <result status="success" msg="成功"/>
    root>
    
    • 1
    • 2
    • 3
    • 4

    2. Java对象

    RootVO.java

    @Data
    @XmlRootElement(name = "root")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class RootVO {
    
        @XmlElement(name = "result")
        public ResultVO result;
    
    
        @Data
        @NoArgsConstructor
        @AllArgsConstructor
        @XmlRootElement(name = "result")
        @XmlAccessorType(XmlAccessType.FIELD)
        public static class ResultVO{
    
            @XmlAttribute(name = "status")
            public String status;
    
            @XmlAttribute(name = "msg")
            public String msg;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    3. 封装的工具类

    XmlUtil.java

    public class XmlUtil {
    
        public static String objToXml(Object obj) {
            try {
                //创建JAXB对象,用于映射java类和xml
                JAXBContext context = JAXBContext.newInstance(obj.getClass());
                //创建Marshaller对象,用于将java对象序列化为xml
                Marshaller marshaller = context.createMarshaller();
                //设置编码格式
                marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
                //格式化生成xml
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                //将java对象序列化为xml
                marshaller.marshal(obj, byteArrayOutputStream);
                return byteArrayOutputStream.toString("UTF-8");
            } catch (JAXBException | UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    4. 测试

       public static void main(String[] args) {
           RootVO rootVO = new RootVO();
           RootVO.ResultVO resultVO = new RootVO.ResultVO("success","成功");
           rootVO.setResult(resultVO);
           String xmlContent = XmlUtil.objToXml(rootVO);
           System.out.println(xmlContent);
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    输出结果:

    
    <root>
        <result status="success" msg="成功"/>
    root>
    
    • 1
    • 2
    • 3
    • 4

    取掉standalone

    	public static void main(String[] args) {
    	    RootVO rootVO = new RootVO();
    	    RootVO.ResultVO resultVO = new RootVO.ResultVO("success","成功");
    	    rootVO.setResult(resultVO);
    	    String xmlContent = XmlUtil.objToXml(rootVO);
    	    System.out.println(xmlContent.replace("standalone=\"yes\"", ""));
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    输出结果:

    
    <root>
        <result status="success" msg="成功"/>
    root>
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    JVM和Java体系结构
    数据结构:链表
    【C++】养很多鱼,只为观察向量的生长
    【C/C++】2024春晚刘谦春晚魔术步骤模拟+暴力破解
    React Hooks的理解
    Oracle EBS 如何提交定时请求(一)-指定某个时间点
    一种基于交叉选择的柯西反向鲸鱼优化算法QOWOA附matlab代码
    R语言读取大型NetCDF文件
    JVM-堆中线程私有空间TLAB(Thread Local Allocation Buffer)
    vscode 使用ES6调试js
  • 原文地址:https://blog.csdn.net/stormkai/article/details/133190490