Xml和Bean互转工具类

/**
 * Xml和Bean互转工具类
 * @author Alex
 * @date 2022/06/02
 */
public class XMLUtil {
    
    
    /**
     * 将对象转换成String类型的xml
     * @param obj 需要转换的对象
     * @return XML格式的字符串
     */
    public static String convertBeanToXml(Object obj) {
        StringWriter result = new StringWriter();
        try {
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(obj, result);
            result.close();
        } catch (JAXBException | IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }
    
    
    /**
     * 讲对象转换成xml文件
     * @param obj  需要转换的对象
     * @param path 输出文件路径
     */
    public static void convertBeanToXml(Object obj, String path) {
        try {
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            FileWriter fw = new FileWriter(path);
            marshaller.marshal(obj, fw);
            fw.flush();
            fw.close();
        } catch (JAXBException | IOException e) {
            e.printStackTrace();
        }
    }
    
    
    /**
     * 将xml字符串转换成对象
     * @param c      需要转换成的对象
     * @param xmlStr 需要转换的xml字符串
     * @return 转换后的对象
     */
    @SuppressWarnings("unchecked")
    public static <T> T convertXmlStrToObject(Class<T> c, String xmlStr) {
        T t = null;
        try {
            JAXBContext context = JAXBContext.newInstance(c);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            StringReader sr = new StringReader(xmlStr);
            t = (T) unmarshaller.unmarshal(sr);
            sr.close();
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return t;
    }
    
    
    /**
     * 将xml文件转换成对象
     * @param c       需要转换成的对象
     * @param xmlPath 需要转换的xml文件路径
     * @return 转换后的对象
     */
    @SuppressWarnings("unchecked")
    public static <T> T convertXmlFileToObject(Class<T> c, String xmlPath) {
        T t = null;
        try {
            JAXBContext context = JAXBContext.newInstance(c);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            FileReader fr = new FileReader(xmlPath);
            t = (T) unmarshaller.unmarshal(fr);
            fr.close();
        } catch (JAXBException | IOException e) {
            e.printStackTrace();
        }
        return t;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的鱼儿~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值