wwl 3 лет назад
Родитель
Сommit
8b4c69828d

+ 68 - 0
twzd-common/src/main/java/com/miaxis/common/utils/XmlUtil.java

@@ -0,0 +1,68 @@
+package com.miaxis.common.utils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import java.io.StringWriter;
+import java.util.Map;
+
+/**
+ * xml util
+ * @author wwl
+ * @version 1.0
+ * @date 2021/10/26 14:55
+ */
+public class XmlUtil {
+
+    private static final Logger log = LoggerFactory.getLogger(XmlUtil.class);
+
+    /**
+     * 将Map转换为XML格式的字符串
+     *
+     * @param data Map类型数据
+     * @return XML格式的字符串
+     * @throws Exception
+     */
+    public static String mapToXml(Map<String, String> data) throws Exception {
+        log.info("----mapToXml-----"+data);
+        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
+        DocumentBuilder documentBuilder= documentBuilderFactory.newDocumentBuilder();
+        org.w3c.dom.Document document = documentBuilder.newDocument();
+        org.w3c.dom.Element root = document.createElement("xml");
+        document.appendChild(root);
+        for (String key: data.keySet()) {
+            String value = data.get(key);
+            if (value == null) {
+                value = "";
+            }
+            value = value.trim();
+            org.w3c.dom.Element filed = document.createElement(key);
+            filed.appendChild(document.createTextNode(value));
+            root.appendChild(filed);
+        }
+        TransformerFactory tf = TransformerFactory.newInstance();
+        Transformer transformer = tf.newTransformer();
+        DOMSource source = new DOMSource(document);
+        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
+        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+        StringWriter writer = new StringWriter();
+        StreamResult result = new StreamResult(writer);
+        transformer.transform(source, result);
+        String output = writer.getBuffer().toString(); //.replaceAll("\n|\r", "");
+        try {
+            writer.close();
+        } catch (Exception ex) {
+
+        }
+        log.info("----mapToXml-return-----"+output);
+        return output;
+    }
+
+}

+ 6 - 6
twzd-common/src/main/java/com/miaxis/common/utils/wx/MessageUtil.java

@@ -1,6 +1,7 @@
 package com.miaxis.common.utils.wx;
 
 import cn.hutool.json.XML;
+import com.miaxis.common.utils.XmlUtil;
 
 import java.util.Date;
 import java.util.HashMap;
@@ -36,7 +37,7 @@ public class MessageUtil {
      * @param content
      * @return
      */
-    public static String initText(String toUserName, String fromUserName, String content) {
+    public static String initText(String toUserName, String fromUserName, String content) throws Exception{
         // 返回消息时ToUserName的值与FromUserName的互换
         Map<String, String> returnMap = new HashMap<>();
         returnMap.put("ToUserName", fromUserName);
@@ -44,8 +45,8 @@ public class MessageUtil {
         returnMap.put("CreateTime", new Date().getTime()+"");
         returnMap.put("MsgType", "text");
         returnMap.put("Content", content);
-        String xmlStr = XML.toXml(returnMap).replace("\"\\\"","").replace("\\\"\"","");
-        return  xmlStr;
+
+        return XmlUtil.mapToXml(returnMap);
     }
 
 
@@ -60,7 +61,7 @@ public class MessageUtil {
      * @param url 点击图文消息跳转链接
      * @return
      */
-    public static String initNews(String toUserName, String fromUserName, String articles, String title, String description, String picUrl, String url) {
+    public static String initNews(String toUserName, String fromUserName, String articles, String title, String description, String picUrl, String url)  throws Exception{
         // 返回消息时ToUserName的值与FromUserName的互换
         Map<String, String> returnMap = new HashMap<>();
         returnMap.put("ToUserName", fromUserName);
@@ -73,8 +74,7 @@ public class MessageUtil {
         returnMap.put("Description", description);
         returnMap.put("PicUrl", picUrl);
         returnMap.put("Url", url);
-        String xmlStr = XML.toXml(returnMap).replace("\"\\\"","").replace("\\\"\"","");
-        return  xmlStr;
+        return XmlUtil.mapToXml(returnMap);
     }