Commit c1d4e1944df7d3fc09bb3058f827ade448d79167
1 parent
18c3b593
增强数据格式校验和转换功能
Showing
2 changed files
with
102 additions
and
37 deletions
src/main/java/com/genersoft/iot/vmp/gb28181/utils/NumericUtil.java
| @@ -15,10 +15,26 @@ public class NumericUtil { | @@ -15,10 +15,26 @@ public class NumericUtil { | ||
| 15 | public static boolean isDouble(String str) { | 15 | public static boolean isDouble(String str) { |
| 16 | try { | 16 | try { |
| 17 | Double num2 = Double.valueOf(str); | 17 | Double num2 = Double.valueOf(str); |
| 18 | - System.out.println("Is Number!" + num2); | 18 | + System.out.println(num2 + " Is an Integer!"); |
| 19 | return true; | 19 | return true; |
| 20 | } catch (Exception e) { | 20 | } catch (Exception e) { |
| 21 | - System.out.println("Is not Number!"); | 21 | + System.out.println(str + " Is not an Integer!"); |
| 22 | + return false; | ||
| 23 | + } | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + /** | ||
| 27 | + * 判断是否Double格式 | ||
| 28 | + * @param str | ||
| 29 | + * @return true/false | ||
| 30 | + */ | ||
| 31 | + public static boolean isInteger(String str) { | ||
| 32 | + try { | ||
| 33 | + int num2 = Integer.valueOf(str); | ||
| 34 | + System.out.println(num2 + " Is Number!"); | ||
| 35 | + return true; | ||
| 36 | + } catch (Exception e) { | ||
| 37 | + System.out.println(str + " Is not Number!"); | ||
| 22 | return false; | 38 | return false; |
| 23 | } | 39 | } |
| 24 | } | 40 | } |
src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
| @@ -7,6 +7,9 @@ import java.util.Iterator; | @@ -7,6 +7,9 @@ import java.util.Iterator; | ||
| 7 | import java.util.List; | 7 | import java.util.List; |
| 8 | import java.util.Map; | 8 | import java.util.Map; |
| 9 | 9 | ||
| 10 | +import com.alibaba.fastjson.JSONArray; | ||
| 11 | +import com.alibaba.fastjson.JSONObject; | ||
| 12 | + | ||
| 10 | import org.dom4j.Attribute; | 13 | import org.dom4j.Attribute; |
| 11 | import org.dom4j.Document; | 14 | import org.dom4j.Document; |
| 12 | import org.dom4j.DocumentException; | 15 | import org.dom4j.DocumentException; |
| @@ -20,8 +23,7 @@ import org.slf4j.LoggerFactory; | @@ -20,8 +23,7 @@ import org.slf4j.LoggerFactory; | ||
| 20 | * | 23 | * |
| 21 | * | 24 | * |
| 22 | */ | 25 | */ |
| 23 | -public class XmlUtil | ||
| 24 | -{ | 26 | +public class XmlUtil { |
| 25 | /** | 27 | /** |
| 26 | * 日志服务 | 28 | * 日志服务 |
| 27 | */ | 29 | */ |
| @@ -30,22 +32,18 @@ public class XmlUtil | @@ -30,22 +32,18 @@ public class XmlUtil | ||
| 30 | /** | 32 | /** |
| 31 | * 解析XML为Document对象 | 33 | * 解析XML为Document对象 |
| 32 | * | 34 | * |
| 33 | - * @param xml | ||
| 34 | - * 被解析的XMl | 35 | + * @param xml 被解析的XMl |
| 36 | + * | ||
| 35 | * @return Document | 37 | * @return Document |
| 36 | */ | 38 | */ |
| 37 | - public static Element parseXml(String xml) | ||
| 38 | - { | 39 | + public static Element parseXml(String xml) { |
| 39 | Document document = null; | 40 | Document document = null; |
| 40 | // | 41 | // |
| 41 | StringReader sr = new StringReader(xml); | 42 | StringReader sr = new StringReader(xml); |
| 42 | SAXReader saxReader = new SAXReader(); | 43 | SAXReader saxReader = new SAXReader(); |
| 43 | - try | ||
| 44 | - { | 44 | + try { |
| 45 | document = saxReader.read(sr); | 45 | document = saxReader.read(sr); |
| 46 | - } | ||
| 47 | - catch (DocumentException e) | ||
| 48 | - { | 46 | + } catch (DocumentException e) { |
| 49 | LOG.error("解析失败", e); | 47 | LOG.error("解析失败", e); |
| 50 | } | 48 | } |
| 51 | return null == document ? null : document.getRootElement(); | 49 | return null == document ? null : document.getRootElement(); |
| @@ -54,16 +52,12 @@ public class XmlUtil | @@ -54,16 +52,12 @@ public class XmlUtil | ||
| 54 | /** | 52 | /** |
| 55 | * 获取element对象的text的值 | 53 | * 获取element对象的text的值 |
| 56 | * | 54 | * |
| 57 | - * @param em | ||
| 58 | - * 节点的对象 | ||
| 59 | - * @param tag | ||
| 60 | - * 节点的tag | 55 | + * @param em 节点的对象 |
| 56 | + * @param tag 节点的tag | ||
| 61 | * @return 节点 | 57 | * @return 节点 |
| 62 | */ | 58 | */ |
| 63 | - public static String getText(Element em, String tag) | ||
| 64 | - { | ||
| 65 | - if (null == em) | ||
| 66 | - { | 59 | + public static String getText(Element em, String tag) { |
| 60 | + if (null == em) { | ||
| 67 | return null; | 61 | return null; |
| 68 | } | 62 | } |
| 69 | Element e = em.element(tag); | 63 | Element e = em.element(tag); |
| @@ -74,16 +68,12 @@ public class XmlUtil | @@ -74,16 +68,12 @@ public class XmlUtil | ||
| 74 | /** | 68 | /** |
| 75 | * 递归解析xml节点,适用于 多节点数据 | 69 | * 递归解析xml节点,适用于 多节点数据 |
| 76 | * | 70 | * |
| 77 | - * @param node | ||
| 78 | - * node | ||
| 79 | - * @param nodeName | ||
| 80 | - * nodeName | 71 | + * @param node node |
| 72 | + * @param nodeName nodeName | ||
| 81 | * @return List<Map<String, Object>> | 73 | * @return List<Map<String, Object>> |
| 82 | */ | 74 | */ |
| 83 | - public static List<Map<String, Object>> listNodes(Element node, String nodeName) | ||
| 84 | - { | ||
| 85 | - if (null == node) | ||
| 86 | - { | 75 | + public static List<Map<String, Object>> listNodes(Element node, String nodeName) { |
| 76 | + if (null == node) { | ||
| 87 | return null; | 77 | return null; |
| 88 | } | 78 | } |
| 89 | // 初始化返回 | 79 | // 初始化返回 |
| @@ -93,12 +83,9 @@ public class XmlUtil | @@ -93,12 +83,9 @@ public class XmlUtil | ||
| 93 | 83 | ||
| 94 | Map<String, Object> map = null; | 84 | Map<String, Object> map = null; |
| 95 | // 遍历属性节点 | 85 | // 遍历属性节点 |
| 96 | - for (Attribute attribute : list) | ||
| 97 | - { | ||
| 98 | - if (nodeName.equals(node.getName())) | ||
| 99 | - { | ||
| 100 | - if (null == map) | ||
| 101 | - { | 86 | + for (Attribute attribute : list) { |
| 87 | + if (nodeName.equals(node.getName())) { | ||
| 88 | + if (null == map) { | ||
| 102 | map = new HashMap<String, Object>(); | 89 | map = new HashMap<String, Object>(); |
| 103 | listMap.add(map); | 90 | listMap.add(map); |
| 104 | } | 91 | } |
| @@ -110,12 +97,74 @@ public class XmlUtil | @@ -110,12 +97,74 @@ public class XmlUtil | ||
| 110 | // 遍历当前节点下的所有节点 ,nodeName 要解析的节点名称 | 97 | // 遍历当前节点下的所有节点 ,nodeName 要解析的节点名称 |
| 111 | // 使用递归 | 98 | // 使用递归 |
| 112 | Iterator<Element> iterator = node.elementIterator(); | 99 | Iterator<Element> iterator = node.elementIterator(); |
| 113 | - while (iterator.hasNext()) | ||
| 114 | - { | 100 | + while (iterator.hasNext()) { |
| 115 | Element e = iterator.next(); | 101 | Element e = iterator.next(); |
| 116 | listMap.addAll(listNodes(e, nodeName)); | 102 | listMap.addAll(listNodes(e, nodeName)); |
| 117 | } | 103 | } |
| 118 | return listMap; | 104 | return listMap; |
| 119 | } | 105 | } |
| 120 | 106 | ||
| 107 | + /** | ||
| 108 | + * xml转json | ||
| 109 | + * | ||
| 110 | + * @param element | ||
| 111 | + * @param json | ||
| 112 | + */ | ||
| 113 | + public static void node2Json(Element element, JSONObject json) { | ||
| 114 | + // 如果是属性 | ||
| 115 | + for (Object o : element.attributes()) { | ||
| 116 | + Attribute attr = (Attribute) o; | ||
| 117 | + if (!isEmpty(attr.getValue())) { | ||
| 118 | + json.put("@" + attr.getName(), attr.getValue()); | ||
| 119 | + } | ||
| 120 | + } | ||
| 121 | + List<Element> chdEl = element.elements(); | ||
| 122 | + if (chdEl.isEmpty() && !isEmpty(element.getText())) {// 如果没有子元素,只有一个值 | ||
| 123 | + json.put(element.getName(), element.getText()); | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + for (Element e : chdEl) { // 有子元素 | ||
| 127 | + if (!e.elements().isEmpty()) { // 子元素也有子元素 | ||
| 128 | + JSONObject chdjson = new JSONObject(); | ||
| 129 | + node2Json(e, chdjson); | ||
| 130 | + Object o = json.get(e.getName()); | ||
| 131 | + if (o != null) { | ||
| 132 | + JSONArray jsona = null; | ||
| 133 | + if (o instanceof JSONObject) { // 如果此元素已存在,则转为jsonArray | ||
| 134 | + JSONObject jsono = (JSONObject) o; | ||
| 135 | + json.remove(e.getName()); | ||
| 136 | + jsona = new JSONArray(); | ||
| 137 | + jsona.add(jsono); | ||
| 138 | + jsona.add(chdjson); | ||
| 139 | + } | ||
| 140 | + if (o instanceof JSONArray) { | ||
| 141 | + jsona = (JSONArray) o; | ||
| 142 | + jsona.add(chdjson); | ||
| 143 | + } | ||
| 144 | + json.put(e.getName(), jsona); | ||
| 145 | + } else { | ||
| 146 | + if (!chdjson.isEmpty()) { | ||
| 147 | + json.put(e.getName(), chdjson); | ||
| 148 | + } | ||
| 149 | + } | ||
| 150 | + } else { // 子元素没有子元素 | ||
| 151 | + for (Object o : element.attributes()) { | ||
| 152 | + Attribute attr = (Attribute) o; | ||
| 153 | + if (!isEmpty(attr.getValue())) { | ||
| 154 | + json.put("@" + attr.getName(), attr.getValue()); | ||
| 155 | + } | ||
| 156 | + } | ||
| 157 | + if (!e.getText().isEmpty()) { | ||
| 158 | + json.put(e.getName(), e.getText()); | ||
| 159 | + } | ||
| 160 | + } | ||
| 161 | + } | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + public static boolean isEmpty(String str) { | ||
| 165 | + if (str == null || str.trim().isEmpty() || "null".equals(str)) { | ||
| 166 | + return true; | ||
| 167 | + } | ||
| 168 | + return false; | ||
| 169 | + } | ||
| 121 | } | 170 | } |