SpringBoot Xml转Json对象
一、导入需要的依赖
<dependency> <groupId>maven</groupId> <artifactId>dom4j</artifactId> <version>1.7-20060614</version> </dependency>
二、xml转Json对象工具类编写
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import java.util.List; /** * @project * @Description XML转json对象工具类 * @Author songwp * @Date 2022/9/20 9:54 * @Version 1.0.0 **/ public class XmltoJsonUtil { /** * 将xml转换为json对象 */ public static JSONObject xmlToJson(String xml) throws DocumentException { JSONObject jsonObject = new JSONObject(); Document document = DocumentHelper.parseText(xml); //获取根节点元素对象 Element root = document.getRootElement(); iterateNodes(root, jsonObject); return jsonObject; } /** * 遍历元素 * @param node * @param json */ private static void iterateNodes(Element node, JSONObject json) { //获取当前元素名称 String nodeName = node.getName(); //判断已遍历的JSON中是否已经有了该元素的名称 if(json.containsKey(nodeName)){ //该元素在同级下有多个 Object Object = json.get(nodeName); JSONArray array = null; if(Object instanceof JSONArray){ array = (JSONArray) Object; }else { array = new JSONArray(); array.add(Object); } //获取该元素下所有子元素 List<Element> listElement = node.elements(); if(listElement.isEmpty()){ //该元素无子元素,获取元素的值 String nodeValue = node.getTextTrim(); array.add(nodeValue); json.put(nodeName, array); return ; } //有子元素 JSONObject newJson = new JSONObject(); //遍历所有子元素 for(Element e:listElement){ //递归 iterateNodes(e,newJson); } array.add(newJson); json.put(nodeName, array); return ; } //该元素同级下第一次遍历 //获取该元素下所有子元素 List<Element> listElement = node.elements(); if(listElement.isEmpty()){ //该元素无子元素,获取元素的值 String nodeValue = node.getTextTrim(); json.put(nodeName, nodeValue); return ; } //有子节点,新建一个JSONObject来存储该节点下子节点的值 JSONObject object = new JSONObject(); for(Element e:listElement){ //递归 iterateNodes(e,object); } json.put(nodeName, object); return ; } public static void main(String[] args) { try { JSONObject object = XmltoJsonUtil.xmlToJson("<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></policyno><policyurl><!--[CDATA[]]--></policyurl></policyinfo><returninfo><serialnumber>2019103015284949545354</serialnumber><retruncode>0</retruncode><errormessage><!--[CDATA[xxx]]--></errormessage></returninfo></auibinsurancecallback>"); System.out.println(object); } catch (DocumentException e) { e.printStackTrace(); } } }
三、方法调用,控制台输出如下:
{ "auibinsurancecallback":{ "returninfo":{ "errormessage":"", "retruncode":"0", "serialnumber":"2019103015284949545354" }, "policyinfo":{ "policyurl":"", "operatetime":"2019-10-16 17:20:00", "transtype":"TKTS", "flightdate":"2019-10-16", "insureno":"1910161720056066735", "flightnumber":"xxx", "agreeno":"102160199", "policyno":"", "eticketno":"xxx" } } }
古今成大事者,不唯有超世之才,必有坚韧不拔之志!