先看效果:
xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?xml version="1.0" encoding="utf-8" ?> <auibinsurancecallback> <policyinfo> <transtype>TKTS</transtype> <eticketno>xxx</eticketno> <flightnumber>xxx</flightnumber> <flightdate>2019-10-16</flightdate> <operatetime>2019-10-16 17:20:00</operatetime> <insureno>1910161720056066735</insureno> <agreeno>102160199</agreeno> <policyno/> <policyurl><![CDATA[]]></policyurl> </policyinfo> <returninfo> <serialnumber>2019103015284949545354</serialnumber> <retruncode>0</retruncode> <errormessage><![CDATA[]]></errormessage> </returninfo> <list> <item> <name>john</name> <age>22</age> <log> <record>1</record> <record>2</record> </log> </item> <item> <name>lucy</name> <age>23</age> <log> <record> <id>1</id> <event>event1</event> </record> <record> <id>2</id> <event>event2</event> </record> </log> </item> </list> <list2> <item> <name>lily</name> <age>24</age> </item> </list2> </auibinsurancecallback> |
json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
{ "auibinsurancecallback": { "policyinfo": { "transtype": "TKTS", "eticketno": "xxx", "flightnumber": "xxx", "flightdate": "2019-10-16", "operatetime": "2019-10-16 17:20:00", "insureno": "1910161720056066735", "agreeno": "102160199", "policyno": "", "policyurl": "" }, "returninfo": { "serialnumber": "2019103015284949545354", "retruncode": "0", "errormessage": "" }, "list": { "item": [ { "name": "john", "age": "22", "log": { "record": [ { "record": "1" }, { "record": "2" } ] } }, { "name": "lucy", "age": "23", "log": { "record": [ { "id": "1", "event": "event1" }, { "id": "2", "event": "event2" } ] } } ] }, "list2": { "item": { "name": "lily", "age": "24" } } } } |
依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!-- https://mvnrepository.com/artifact/org.dom4j/dom4j --> <dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> <version>2.1.4</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 --> <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>2.0.42</version> </dependency> |
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
package org.longsheng.util; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import lombok.extern.slf4j.Slf4j; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import java.util.ArrayList; import java.util.List; /** * Xml转JSON工具包 */ @Slf4j public class XmlToJsonUtil { /** * 转换为json字符串 * * @param xmlString xml字符串 * @return json字符串 */ public static String toJsonString(String xmlString) { JSONObject result = toJson(xmlString); if (result == null) { return null; } return result.toString(); } /** * 转换为json对象 * * @param xmlString xml字符串 * @return json对象 */ public static JSONObject toJson(String xmlString) { try { Document doc = DocumentHelper.parseText(xmlString); return toJson(doc); } catch (DocumentException e) { log.error("{}===>{}===>exception: {} | {}", Thread.currentThread().getStackTrace()[1].getClassName(), Thread.currentThread().getStackTrace()[1].getMethodName(), e.getMessage(), e.getStackTrace()); } return null; } /** * 转换为json对象 * * @param document xml对象 * @return json对象 */ public static JSONObject toJson(Document document) { if (document == null) { return null; } try { Element root = document.getRootElement(); List<Element> elements = root.elements(); JSONObject jsonObject = new JSONObject(); jsonObject.put(root.getName(), elementToJson(elements)); return jsonObject; } catch (Exception e) { log.error("{}===>{}===>exception: {} | {}", Thread.currentThread().getStackTrace()[1].getClassName(), Thread.currentThread().getStackTrace()[1].getMethodName(), e.getMessage(), e.getStackTrace()); } return null; } /** * 递归读取节点,并简单判定节点状态(值、对象、列表三种状态) * * @param elements * @return */ public static JSONObject elementToJson(List<Element> elements) { if (elements == null || elements.isEmpty()) { return null; } JSONObject result = new JSONObject(); // single object if (elements.size() == 1) { Element element = elements.get(0); List<Element> children = element.elements(); result.put(element.getName(), children.isEmpty() ? element.getText() : elementToJson(children)); return result; } // multi-object if (!elements.get(0).getName().equals(elements.get(1).getName())) { result.clear(); List<Element> children; for (Element element : elements) { children = element.elements(); result.put(element.getName(), children.isEmpty() ? element.getText() : elementToJson(children)); } return result; } // array list List<JSONObject> jsonList = new ArrayList<>(); List<Element> children; for (Element element : elements) { children = element.elements(); result = new JSONObject(); if (children.isEmpty()) { result.put(element.getName(), element.getText()); } else { result = elementToJson(children); } jsonList.add(result); } result = new JSONObject(); result.put(elements.get(0).getName(), jsonList); return result; } /** * 从json对象中获取指定名称的json数组 * * @param jsonObject json对象 * @param nodeName json数组节点名称 * @return json数组 */ public static JSONArray getArray(JSONObject jsonObject, String nodeName) { if (jsonObject == null || nodeName == null || nodeName.isEmpty()) { return null; } Object node = jsonObject.get(nodeName); if (node == null) { return null; } JSONArray result = new JSONArray(); if (node instanceof JSONObject) { result.add(node); return result; } if (node instanceof ArrayList) { result = jsonObject.getJSONArray(nodeName); } return result; } public static void main(String[] args) { String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" + "<auibinsurancecallback>\n" + "\t<policyinfo>\n" + "\t\t<transtype>TKTS</transtype>\n" + "\t\t<eticketno>xxx</eticketno>\n" + "\t\t<flightnumber>xxx</flightnumber>\n" + "\t\t<flightdate>2019-10-16</flightdate>\n" + "\t\t<operatetime>2019-10-16 17:20:00</operatetime>\n" + "\t\t<insureno>1910161720056066735</insureno>\n" + "\t\t<agreeno>102160199</agreeno>\n" + "\t\t<policyno/>\n" + "\t\t<policyurl><![CDATA[]]></policyurl>\n" + "\t</policyinfo>\n" + "\t<returninfo>\n" + "\t\t<serialnumber>2019103015284949545354</serialnumber>\n" + "\t\t<retruncode>0</retruncode>\n" + "\t\t<errormessage><![CDATA[]]></errormessage>\n" + "\t</returninfo>\n" + "\t<list>\n" + "\t\t<item>" + "\t\t\t<name>john</name>" + "\t\t\t<age>22</age>" + "\t\t\t<log>" + "\t\t\t\t<record>1</record>" + "\t\t\t\t<record>2</record>" + "\t\t\t</log>" + "\t\t</item>\n" + "\t\t<item>" + "\t\t\t<name>lucy</name>" + "\t\t\t<age>23</age>" + "\t\t\t<log>" + "\t\t\t\t<record>" + "\t\t\t\t\t<id>1</id>" + "\t\t\t\t\t<event>event1</event>" + "\t\t\t\t</record>" + "\t\t\t\t<record>" + "\t\t\t\t\t<id>2</id>" + "\t\t\t\t\t<event>event2</event>" + "\t\t\t\t</record>" + "\t\t\t</log>" + "\t\t</item>\n" + "\t</list>\n" + "\t<list2>\n" + "\t\t<item>" + "\t\t\t<name>lily</name>" + "\t\t\t<age>24</age>" + "\t\t</item>\n" + "\t</list2>\n" + "</auibinsurancecallback>"; JSONObject json = toJson(xml); System.out.println(xml); System.out.println(); assert json != null; System.out.println(json.toJSONString()); JSONArray array1 = getArray(json.getJSONObject("auibinsurancecallback").getJSONObject("list"), "item"); JSONArray array2 = getArray(json.getJSONObject("auibinsurancecallback").getJSONObject("list2"), "item"); System.out.println(); System.out.println(array1.toJSONString()); System.out.println(array2.toJSONString()); } } |