XmlUtil.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package com.genersoft.iot.vmp.gb28181.utils;
  2. import java.io.StringReader;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Map;
  8. import com.alibaba.fastjson.JSONArray;
  9. import com.alibaba.fastjson.JSONObject;
  10. import org.dom4j.Attribute;
  11. import org.dom4j.Document;
  12. import org.dom4j.DocumentException;
  13. import org.dom4j.Element;
  14. import org.dom4j.io.SAXReader;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.util.StringUtils;
  18. /**
  19. * 基于dom4j的工具包
  20. *
  21. *
  22. */
  23. public class XmlUtil {
  24. /**
  25. * 日志服务
  26. */
  27. private static Logger LOG = LoggerFactory.getLogger(XmlUtil.class);
  28. /**
  29. * 解析XML为Document对象
  30. *
  31. * @param xml 被解析的XMl
  32. *
  33. * @return Document
  34. */
  35. public static Element parseXml(String xml) {
  36. Document document = null;
  37. //
  38. StringReader sr = new StringReader(xml);
  39. SAXReader saxReader = new SAXReader();
  40. try {
  41. document = saxReader.read(sr);
  42. } catch (DocumentException e) {
  43. LOG.error("解析失败", e);
  44. }
  45. return null == document ? null : document.getRootElement();
  46. }
  47. /**
  48. * 获取element对象的text的值
  49. *
  50. * @param em 节点的对象
  51. * @param tag 节点的tag
  52. * @return 节点
  53. */
  54. public static String getText(Element em, String tag) {
  55. if (null == em) {
  56. return null;
  57. }
  58. Element e = em.element(tag);
  59. //
  60. return null == e ? null : e.getText();
  61. }
  62. /**
  63. * 递归解析xml节点,适用于 多节点数据
  64. *
  65. * @param node node
  66. * @param nodeName nodeName
  67. * @return List<Map<String, Object>>
  68. */
  69. public static List<Map<String, Object>> listNodes(Element node, String nodeName) {
  70. if (null == node) {
  71. return null;
  72. }
  73. // 初始化返回
  74. List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
  75. // 首先获取当前节点的所有属性节点
  76. List<Attribute> list = node.attributes();
  77. Map<String, Object> map = null;
  78. // 遍历属性节点
  79. for (Attribute attribute : list) {
  80. if (nodeName.equals(node.getName())) {
  81. if (null == map) {
  82. map = new HashMap<String, Object>();
  83. listMap.add(map);
  84. }
  85. // 取到的节点属性放到map中
  86. map.put(attribute.getName(), attribute.getValue());
  87. }
  88. }
  89. // 遍历当前节点下的所有节点 ,nodeName 要解析的节点名称
  90. // 使用递归
  91. Iterator<Element> iterator = node.elementIterator();
  92. while (iterator.hasNext()) {
  93. Element e = iterator.next();
  94. listMap.addAll(listNodes(e, nodeName));
  95. }
  96. return listMap;
  97. }
  98. /**
  99. * xml转json
  100. *
  101. * @param element
  102. * @param json
  103. */
  104. public static void node2Json(Element element, JSONObject json) {
  105. // 如果是属性
  106. for (Object o : element.attributes()) {
  107. Attribute attr = (Attribute) o;
  108. if (!StringUtils.isEmpty(attr.getValue())) {
  109. json.put("@" + attr.getName(), attr.getValue());
  110. }
  111. }
  112. List<Element> chdEl = element.elements();
  113. if (chdEl.isEmpty() && !StringUtils.isEmpty(element.getText())) {// 如果没有子元素,只有一个值
  114. json.put(element.getName(), element.getText());
  115. }
  116. for (Element e : chdEl) { // 有子元素
  117. if (!e.elements().isEmpty()) { // 子元素也有子元素
  118. JSONObject chdjson = new JSONObject();
  119. node2Json(e, chdjson);
  120. Object o = json.get(e.getName());
  121. if (o != null) {
  122. JSONArray jsona = null;
  123. if (o instanceof JSONObject) { // 如果此元素已存在,则转为jsonArray
  124. JSONObject jsono = (JSONObject) o;
  125. json.remove(e.getName());
  126. jsona = new JSONArray();
  127. jsona.add(jsono);
  128. jsona.add(chdjson);
  129. }
  130. if (o instanceof JSONArray) {
  131. jsona = (JSONArray) o;
  132. jsona.add(chdjson);
  133. }
  134. json.put(e.getName(), jsona);
  135. } else {
  136. if (!chdjson.isEmpty()) {
  137. json.put(e.getName(), chdjson);
  138. }
  139. }
  140. } else { // 子元素没有子元素
  141. for (Object o : element.attributes()) {
  142. Attribute attr = (Attribute) o;
  143. if (!StringUtils.isEmpty(attr.getValue())) {
  144. json.put("@" + attr.getName(), attr.getValue());
  145. }
  146. }
  147. if (!e.getText().isEmpty()) {
  148. json.put(e.getName(), e.getText());
  149. }
  150. }
  151. }
  152. }
  153. }