From 1401ce98d480cc4c77507d86dc35c939a5becfe9 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期三, 15 十二月 2021 11:46:26 +0800
Subject: [PATCH] 优化mysql脚本
---
src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java | 144 ++++++++++++++++++++++++++++++++++--------------
1 files changed, 102 insertions(+), 42 deletions(-)
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
index e0d776c..079a78b 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
@@ -1,12 +1,7 @@
package com.genersoft.iot.vmp.gb28181.utils;
-import java.io.StringReader;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
@@ -14,14 +9,20 @@
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.util.StringUtils;
+
+import javax.sip.RequestEvent;
+import javax.sip.message.Request;
+import java.io.ByteArrayInputStream;
+import java.io.StringReader;
+import java.util.*;
/**
* 鍩轰簬dom4j鐨勫伐鍏峰寘
*
*
*/
-public class XmlUtil
-{
+public class XmlUtil {
/**
* 鏃ュ織鏈嶅姟
*/
@@ -30,22 +31,18 @@
/**
* 瑙f瀽XML涓篋ocument瀵硅薄
*
- * @param xml
- * 琚В鏋愮殑XMl
+ * @param xml 琚В鏋愮殑XMl
+ *
* @return Document
*/
- public static Element parseXml(String xml)
- {
+ public static Element parseXml(String xml) {
Document document = null;
//
StringReader sr = new StringReader(xml);
SAXReader saxReader = new SAXReader();
- try
- {
+ try {
document = saxReader.read(sr);
- }
- catch (DocumentException e)
- {
+ } catch (DocumentException e) {
LOG.error("瑙f瀽澶辫触", e);
}
return null == document ? null : document.getRootElement();
@@ -54,16 +51,12 @@
/**
* 鑾峰彇element瀵硅薄鐨則ext鐨勫��
*
- * @param em
- * 鑺傜偣鐨勫璞�
- * @param tag
- * 鑺傜偣鐨則ag
+ * @param em 鑺傜偣鐨勫璞�
+ * @param tag 鑺傜偣鐨則ag
* @return 鑺傜偣
*/
- public static String getText(Element em, String tag)
- {
- if (null == em)
- {
+ public static String getText(Element em, String tag) {
+ if (null == em) {
return null;
}
Element e = em.element(tag);
@@ -74,16 +67,12 @@
/**
* 閫掑綊瑙f瀽xml鑺傜偣锛岄�傜敤浜� 澶氳妭鐐规暟鎹�
*
- * @param node
- * node
- * @param nodeName
- * nodeName
+ * @param node node
+ * @param nodeName nodeName
* @return List<Map<String, Object>>
*/
- public static List<Map<String, Object>> listNodes(Element node, String nodeName)
- {
- if (null == node)
- {
+ public static List<Map<String, Object>> listNodes(Element node, String nodeName) {
+ if (null == node) {
return null;
}
// 鍒濆鍖栬繑鍥�
@@ -93,12 +82,9 @@
Map<String, Object> map = null;
// 閬嶅巻灞炴�ц妭鐐�
- for (Attribute attribute : list)
- {
- if (nodeName.equals(node.getName()))
- {
- if (null == map)
- {
+ for (Attribute attribute : list) {
+ if (nodeName.equals(node.getName())) {
+ if (null == map) {
map = new HashMap<String, Object>();
listMap.add(map);
}
@@ -110,12 +96,86 @@
// 閬嶅巻褰撳墠鑺傜偣涓嬬殑鎵�鏈夎妭鐐� 锛宯odeName 瑕佽В鏋愮殑鑺傜偣鍚嶇О
// 浣跨敤閫掑綊
Iterator<Element> iterator = node.elementIterator();
- while (iterator.hasNext())
- {
+ while (iterator.hasNext()) {
Element e = iterator.next();
listMap.addAll(listNodes(e, nodeName));
}
return listMap;
}
+ /**
+ * xml杞琷son
+ *
+ * @param element
+ * @param json
+ */
+ public static void node2Json(Element element, JSONObject json) {
+ // 濡傛灉鏄睘鎬�
+ for (Object o : element.attributes()) {
+ Attribute attr = (Attribute) o;
+ if (!StringUtils.isEmpty(attr.getValue())) {
+ json.put("@" + attr.getName(), attr.getValue());
+ }
+ }
+ List<Element> chdEl = element.elements();
+ if (chdEl.isEmpty() && !StringUtils.isEmpty(element.getText())) {// 濡傛灉娌℃湁瀛愬厓绱�,鍙湁涓�涓��
+ json.put(element.getName(), element.getText());
+ }
+
+ for (Element e : chdEl) { // 鏈夊瓙鍏冪礌
+ if (!e.elements().isEmpty()) { // 瀛愬厓绱犱篃鏈夊瓙鍏冪礌
+ JSONObject chdjson = new JSONObject();
+ node2Json(e, chdjson);
+ Object o = json.get(e.getName());
+ if (o != null) {
+ JSONArray jsona = null;
+ if (o instanceof JSONObject) { // 濡傛灉姝ゅ厓绱犲凡瀛樺湪,鍒欒浆涓簀sonArray
+ JSONObject jsono = (JSONObject) o;
+ json.remove(e.getName());
+ jsona = new JSONArray();
+ jsona.add(jsono);
+ jsona.add(chdjson);
+ }
+ if (o instanceof JSONArray) {
+ jsona = (JSONArray) o;
+ jsona.add(chdjson);
+ }
+ json.put(e.getName(), jsona);
+ } else {
+ if (!chdjson.isEmpty()) {
+ json.put(e.getName(), chdjson);
+ }
+ }
+ } else { // 瀛愬厓绱犳病鏈夊瓙鍏冪礌
+ for (Object o : element.attributes()) {
+ Attribute attr = (Attribute) o;
+ if (!StringUtils.isEmpty(attr.getValue())) {
+ json.put("@" + attr.getName(), attr.getValue());
+ }
+ }
+ if (!e.getText().isEmpty()) {
+ json.put(e.getName(), e.getText());
+ }
+ }
+ }
+ }
+ public static Element getRootElement(RequestEvent evt) throws DocumentException {
+
+ return getRootElement(evt, "gb2312");
+ }
+
+ public static Element getRootElement(RequestEvent evt, String charset) throws DocumentException {
+ Request request = evt.getRequest();
+ return getRootElement(request.getRawContent(), charset);
+ }
+
+ public static Element getRootElement(byte[] content, String charset) throws DocumentException {
+ if (charset == null) {
+ charset = "gb2312";
+ }
+ SAXReader reader = new SAXReader();
+ reader.setEncoding(charset);
+ Document xml = reader.read(new ByteArrayInputStream(content));
+ return xml.getRootElement();
+ }
}
--
Gitblit v1.8.0