使用JDK自带的JAXB进行实体类与XML的相互转换工具类_@起风了的博客-CSDN博客

package com.mywork.util;

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.*;

@Slf4j
public class XMLUtil {

    /**
     * 对象转XML
     * @param obj 目标对象
     * @return  返回string格式的xml报文
     */
    public static String objToXml(Object obj){
        StringWriter sw = new StringWriter();
        String result = null;
        try {
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
            marshaller.marshal(obj,sw);
            result = sw.toString() ;
        } catch (JAXBException e) {
            log.error("对象转XML异常:{}",e.getMessage());
            e.printStackTrace();
        }finally {
            try {
                sw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * XML转对象
     * @param clazz 需要转换成对象的class
     * @param xml   目标xml报文
     */
    public static <T> T xmlToObj(Class clazz, String xml){
        Object obj = null;
        StringReader sr = null ;
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            sr = new StringReader(xml);
            obj = unmarshaller.unmarshal(sr);
        } catch (JAXBException e) {
            log.error("XML转对象异常:{}",e.getMessage());
            e.printStackTrace();
        }finally {
            if (sr != null){
                sr.close();
            }
        }
        return (T) obj;
    }


    /**
     * 对象转XML文件
     * @param obj
     * @param path
     * @return
     */
    public static String objToXmlFile(Object obj , String path){
        FileWriter fw = null;
        String result = null;
        try {
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
            fw = new FileWriter(path);
            marshaller.marshal(obj,fw);
            result = fw.toString();
        } catch (JAXBException | IOException e) {
            log.error("对象转XML文件异常:{}",e.getMessage());
        }finally {
            if (fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }



    /**
     * XML文件转成对象
     * @param clazz
     * @param xmlPath   文件路径
     * @param <T>
     * @return
     */
    public static <T> T xmlFileToObj(Class<T> clazz,String xmlPath){
        Object obj = null;
        FileReader fr = null;
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            fr = new FileReader(xmlPath);
            obj = unmarshaller.unmarshal(fr);
        } catch (JAXBException | FileNotFoundException e) {
            log.error("file类型的xml转换对象错误:{}",e.getMessage());
        }finally {
            if (fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    log.error("关闭流异常:{}",e.getMessage());
                    e.printStackTrace();
                }
            }
        }
        return (T) obj;
    }
}

存在问题:
目前还没发现可以使用JAXB将一个简单的List<T>(T非自定义的实体类,例如List<String>List<Integer>),直接转化为XML


原网址: 访问
创建于: 2022-05-05 14:04:47
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论