|
@@ -2,6 +2,14 @@ package com.miaxis.common.utils.wx;
|
|
|
|
|
|
import com.miaxis.common.utils.XmlUtil;
|
|
|
|
|
|
+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.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
@@ -45,7 +53,7 @@ public class MessageUtil {
|
|
|
returnMap.put("MsgType", "text");
|
|
|
returnMap.put("Content", content);
|
|
|
|
|
|
- return XmlUtil.mapToXml(returnMap);
|
|
|
+ return mapToXml(returnMap);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -73,7 +81,49 @@ public class MessageUtil {
|
|
|
returnMap.put("Description", description);
|
|
|
returnMap.put("PicUrl", picUrl);
|
|
|
returnMap.put("Url", url);
|
|
|
- return XmlUtil.mapToXml(returnMap);
|
|
|
+ return mapToXml(returnMap);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Map转换为XML格式的字符串
|
|
|
+ *
|
|
|
+ * @param data Map类型数据
|
|
|
+ * @return XML格式的字符串
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String mapToXml(Map<String, String> data) throws Exception {
|
|
|
+
|
|
|
+ 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) {
|
|
|
+
|
|
|
+ }
|
|
|
+ return output;
|
|
|
}
|
|
|
|
|
|
|