From 38914a6947d4cb76577fa97432d810ec66a870fe Mon Sep 17 00:00:00 2001
From: ChenJiaHe <763432473@qq.com>
Date: 星期四, 21 一月 2021 11:16:05 +0800
Subject: [PATCH] XML工具

---
 src/main/java/com/hx/util/XmlUtil.java |  113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 pom.xml                                |    7 +++
 2 files changed, 120 insertions(+), 0 deletions(-)

diff --git a/pom.xml b/pom.xml
index 1be7491..7601292 100644
--- a/pom.xml
+++ b/pom.xml
@@ -15,6 +15,13 @@
 
     <dependencies>
 
+        <!--瀵硅薄涓嶺ML澶勭悊-->
+        <dependency>
+            <groupId>javax.xml.bind</groupId>
+            <artifactId>jaxb-api</artifactId>
+        </dependency>
+
+
         <!--鑵捐浜� cos-->
         <dependency>
             <groupId>com.qcloud</groupId>
diff --git a/src/main/java/com/hx/util/XmlUtil.java b/src/main/java/com/hx/util/XmlUtil.java
new file mode 100644
index 0000000..6ad4c95
--- /dev/null
+++ b/src/main/java/com/hx/util/XmlUtil.java
@@ -0,0 +1,113 @@
+package com.hx.util;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import java.io.*;
+
+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);
+            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// //缂栫爜鏍煎紡
+            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 鏄惁鏍煎紡鍖栫敓鎴愮殑xml涓�
+            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// 鏄惁鐪佺暐xm澶村0鏄庝俊鎭�
+            // 灏嗗璞¤浆鎹㈡垚杈撳嚭娴佸舰寮忕殑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);
+            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// //缂栫爜鏍煎紡
+            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 鏄惁鏍煎紡鍖栫敓鎴愮殑xml涓�
+            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// 鏄惁鐪佺暐xm澶村0鏄庝俊鎭�
+            // 灏嗗璞¤浆鎹㈡垚杈撳嚭娴佸舰寮忕殑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.8.0