From c1d4e1944df7d3fc09bb3058f827ade448d79167 Mon Sep 17 00:00:00 2001 From: lawrencehj <1934378145@qq.com> Date: Wed, 3 Feb 2021 11:05:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E6=95=B0=E6=8D=AE=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E6=A0=A1=E9=AA=8C=E5=92=8C=E8=BD=AC=E6=8D=A2=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iot/vmp/gb28181/utils/NumericUtil.java | 20 ++- .../iot/vmp/gb28181/utils/XmlUtil.java | 119 ++++++++++++------ 2 files changed, 102 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/NumericUtil.java b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/NumericUtil.java index 4dc63075..f9fbfb18 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/NumericUtil.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/NumericUtil.java @@ -15,10 +15,26 @@ public class NumericUtil { public static boolean isDouble(String str) { try { Double num2 = Double.valueOf(str); - System.out.println("Is Number!" + num2); + System.out.println(num2 + " Is an Integer!"); return true; } catch (Exception e) { - System.out.println("Is not Number!"); + System.out.println(str + " Is not an Integer!"); + return false; + } + } + + /** + * 判断是否Double格式 + * @param str + * @return true/false + */ + public static boolean isInteger(String str) { + try { + int num2 = Integer.valueOf(str); + System.out.println(num2 + " Is Number!"); + return true; + } catch (Exception e) { + System.out.println(str + " Is not Number!"); return false; } } 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 e0d776c2..c82ba600 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.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; @@ -20,8 +23,7 @@ import org.slf4j.LoggerFactory; * * */ -public class XmlUtil -{ +public class XmlUtil { /** * 日志服务 */ @@ -30,22 +32,18 @@ public class XmlUtil /** * 解析XML为Document对象 * - * @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("解析失败", e); } return null == document ? null : document.getRootElement(); @@ -54,16 +52,12 @@ public class XmlUtil /** * 获取element对象的text的值 * - * @param em - * 节点的对象 - * @param tag - * 节点的tag + * @param em 节点的对象 + * @param tag 节点的tag * @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 @@ public class XmlUtil /** * 递归解析xml节点,适用于 多节点数据 * - * @param node - * node - * @param nodeName - * nodeName + * @param node node + * @param nodeName nodeName * @return List> */ - public static List> listNodes(Element node, String nodeName) - { - if (null == node) - { + public static List> listNodes(Element node, String nodeName) { + if (null == node) { return null; } // 初始化返回 @@ -93,12 +83,9 @@ public class XmlUtil Map 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(); listMap.add(map); } @@ -110,12 +97,74 @@ public class XmlUtil // 遍历当前节点下的所有节点 ,nodeName 要解析的节点名称 // 使用递归 Iterator iterator = node.elementIterator(); - while (iterator.hasNext()) - { + while (iterator.hasNext()) { Element e = iterator.next(); listMap.addAll(listNodes(e, nodeName)); } return listMap; } + /** + * xml转json + * + * @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 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) { // 如果此元素已存在,则转为jsonArray + 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; + } }