From d3fa1dd2262fc311661aa675e4a30b2423c7b130 Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: 星期四, 04 二月 2021 10:35:02 +0800 Subject: [PATCH] Merge pull request #47 from lawrencehj/master --- src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java | 119 ++++++++++++++++++++++++++++++++++++++++++----------------- 1 files changed, 84 insertions(+), 35 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..c82ba60 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 @@ -7,6 +7,9 @@ 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; @@ -20,8 +23,7 @@ * * */ -public class XmlUtil -{ +public class XmlUtil { /** * 鏃ュ織鏈嶅姟 */ @@ -30,22 +32,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 +52,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 +68,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 +83,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 +97,74 @@ // 閬嶅巻褰撳墠鑺傜偣涓嬬殑鎵�鏈夎妭鐐� 锛宯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 (!isEmpty(attr.getValue())) { + json.put("@" + attr.getName(), attr.getValue()); + } + } + List<Element> chdEl = element.elements(); + if (chdEl.isEmpty() && !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 (!isEmpty(attr.getValue())) { + json.put("@" + attr.getName(), attr.getValue()); + } + } + if (!e.getText().isEmpty()) { + json.put(e.getName(), e.getText()); + } + } + } + } + + public static boolean isEmpty(String str) { + if (str == null || str.trim().isEmpty() || "null".equals(str)) { + return true; + } + return false; + } } -- Gitblit v1.8.0