From 3b2decedde042efe481330a20981a374efdf5978 Mon Sep 17 00:00:00 2001 From: Lius <Lius2225@163.com> Date: 星期二, 18 六月 2024 10:39:33 +0800 Subject: [PATCH] 提交 --- lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/util/XmlUtil.java | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 110 insertions(+), 0 deletions(-) diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/util/XmlUtil.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/util/XmlUtil.java new file mode 100644 index 0000000..a8f0b00 --- /dev/null +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/util/XmlUtil.java @@ -0,0 +1,110 @@ +package org.jeecg.modules.mdc.util; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; +import java.io.*; + +/** + * @author Lius + * @date 2024/6/12 10:31 + */ +public class XmlUtil { + /** + * 灏嗗璞$洿鎺ヨ浆鎹㈡垚String绫诲瀷鐨� XML杈撳嚭 + * + * @param obj + * @return + */ + public static String convertToXml(Object obj) { + // 鍒涘缓杈撳嚭娴� + StringWriter sw = new StringWriter(); + try { + // 鍒╃敤jdk涓嚜甯︾殑杞崲绫诲疄鐜� + JAXBContext context = JAXBContext.newInstance(obj.getClass()); + + Marshaller marshaller = context.createMarshaller(); + // 鏍煎紡鍖杧ml杈撳嚭鐨勬牸寮� +// marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); + // 鍘绘帀鐢熸垚xml鏃剁殑榛樿鎶ユ枃澶� + marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); + // 灏嗗璞¤浆鎹㈡垚杈撳嚭娴佸舰寮忕殑xml + marshaller.marshal(obj, sw); + } catch (JAXBException e) { + e.printStackTrace(); + } + return sw.toString(); + } + + /** + * 灏嗗璞℃牴鎹矾寰勮浆鎹㈡垚xml鏂囦欢 + * + * @param obj + * @param path + * @return + */ + public static void convertToXml(Object obj, String path) { + try { + // 鍒╃敤jdk涓嚜甯︾殑杞崲绫诲疄鐜� + JAXBContext context = JAXBContext.newInstance(obj.getClass()); + + Marshaller marshaller = context.createMarshaller(); + // 鏍煎紡鍖杧ml杈撳嚭鐨勬牸寮� + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, + Boolean.TRUE); + // 灏嗗璞¤浆鎹㈡垚杈撳嚭娴佸舰寮忕殑xml + // 鍒涘缓杈撳嚭娴� + FileWriter fw = null; + try { + fw = new FileWriter(path); + } catch (IOException e) { + e.printStackTrace(); + } + marshaller.marshal(obj, fw); + } catch (JAXBException e) { + e.printStackTrace(); + } + } + + @SuppressWarnings("unchecked") + /** + * 灏哠tring绫诲瀷鐨剎ml杞崲鎴愬璞� + */ + public static Object convertXmlStrToObject(Class clazz, String xmlStr) { + Object xmlObject = null; + try { + JAXBContext context = JAXBContext.newInstance(clazz); + // 杩涜灏哫ml杞垚瀵硅薄鐨勬牳蹇冩帴鍙� + Unmarshaller unmarshaller = context.createUnmarshaller(); + StringReader sr = new StringReader(xmlStr); + xmlObject = unmarshaller.unmarshal(sr); + } catch (JAXBException e) { + e.printStackTrace(); + } + return xmlObject; + } + + @SuppressWarnings("unchecked") + /** + * 灏唂ile绫诲瀷鐨剎ml杞崲鎴愬璞� + */ + public static Object convertXmlFileToObject(Class clazz, String xmlPath) { + Object xmlObject = null; + try { + JAXBContext context = JAXBContext.newInstance(clazz); + Unmarshaller unmarshaller = context.createUnmarshaller(); + FileReader fr = null; + try { + fr = new FileReader(xmlPath); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + xmlObject = unmarshaller.unmarshal(fr); + } catch (JAXBException e) { + e.printStackTrace(); + } + return xmlObject; + } + +} -- Gitblit v1.9.3