结果
原始
# 原始
{"head":{"opCode":"0202","bankCode":"04160000","cityCode":"370800"},"param":{"accountNo":"A20220101","accountMainNo":"A202201","type":"0","contractNo":"CN20220923050601","tradeAmount":"2000000","mode":"01","bankSerial":"SS2022092317210102","settle":"02","outletNo":"","tellerNo":""}}
# 转xml后
<?xml version='1.0' encoding='UTF-8'?><root><Head><opCode>0202</opCode><bankCode>04160000</bankCode><cityCode>370800</cityCode></Head><Param><accountNo>A20220101</accountNo><accountMainNo>A202201</accountMainNo><type>0</type><contractNo>CN20220923050601</contractNo><tradeAmount>2000000</tradeAmount><mode>01</mode><bankSerial>SS2022092317210102</bankSerial><settle>02</settle><outletNo></outletNo><tellerNo></tellerNo></Param></root>
# xml转json后
{"head":{"bankCode":"04160000","cityCode":"370800","opCode":"0202"},"param":{"mode":"01","tradeAmount":"2000000","contractNo":"CN20220923050601","accountNo":"A20220101","settle":"02","type":"0","accountMainNo":"A202201","bankSerial":"SS2022092317210102"}}
引入依赖
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>
工具类
package com.chinaums.mqy.util;
import com.google.gson.*;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.util.*;
/**
* xml和json互转工具类
*
* @author DaenMax
*/
public class XmlToJsonUtil {
/**
* json转到xml
*
* @param json
* @return
*/
public static String jsonToXml(String json) {
Document document = DocumentHelper.createDocument();
Element root = document.addElement("root"); //添加一个根节点,根据你自己的需求进行修改
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
Element ee = toXml(jsonObject, root, null);
String xml = ee.asXML();
//下面这行的拼接头部和替换大小写根据自己的需求进行抉择是否删除
String replace = "<?xml version='1.0' encoding='UTF-8'?>" + xml.replace("head>", "Head>").replace("param>", "Param>");
return replace;
}
private static Element toXml(JsonElement jsonElement, Element parentElement, String name) {
if (jsonElement instanceof JsonArray) {
//是json数据,需继续解析
JsonArray sonJsonArray = (JsonArray) jsonElement;
for (int i = 0; i < sonJsonArray.size(); i++) {
JsonElement arrayElement = sonJsonArray.get(i);
toXml(arrayElement, parentElement, name);
}
} else if (jsonElement instanceof JsonObject) {
//说明是一个json对象字符串,需要继续解析
JsonObject sonJsonObject = (JsonObject) jsonElement;
Element currentElement = null;
if (name != null) {
currentElement = parentElement.addElement(name);
}
Set<Map.Entry<String, JsonElement>> set = sonJsonObject.entrySet();
for (Map.Entry<String, JsonElement> s : set) {
toXml(s.getValue(), currentElement != null ? currentElement : parentElement, s.getKey());
}
} else {
//说明是一个键值对的key,可以作为节点插入了
addAttribute(parentElement, name, jsonElement.getAsString());
}
return parentElement;
}
private static void addAttribute(Element element, String name, String value) {
//增加子节点,并为子节点赋值
Element el = element.addElement(name);
el.addText(value);
}
/**
* xml转到json
*
* @param xml
* @return
*/
public static String xmlToJson(String xml) {
Map<String, Object> map = new HashMap<>();
String xmlR = xml;
//下面这行的替换大小写根据自己的需求进行抉择是否删除
xmlR = xmlR.replace("Head>", "head>").replace("Param>", "param>");
try {
Document doc = DocumentHelper.parseText(xmlR);
Element root = doc.getRootElement();
map.put(root.getName(), iterateElement(root));
//这里随便找个算法进行map转到json返回即可,是否直接全部取还是取root根据自己的需求进行抉择是否删除
String json = JsonUtils.toJson(map.get("root"));
return json;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static Map iterateElement(Element element) {
List el = element.elements();
Element et = null;
Map obj = new HashMap();
Object temp;
List list = null;
for (int i = 0; i < el.size(); i++) {
list = new LinkedList();
et = (Element) el.get(i);
if (et.getTextTrim().equals("")) {
if (et.elements().size() == 0)
continue;
if (obj.containsKey(et.getName())) {
temp = obj.get(et.getName());
if (temp instanceof List) {
list = (List) temp;
list.add(iterateElement(et));
} else if (temp instanceof Map) {
list.add((HashMap) temp);
list.add(iterateElement(et));
} else {
list.add((String) temp);
list.add(iterateElement(et));
}
obj.put(et.getName(), list);
} else {
obj.put(et.getName(), iterateElement(et));
}
} else {
if (obj.containsKey(et.getName())) {
temp = obj.get(et.getName());
if (temp instanceof List) {
list = (List) temp;
list.add(et.getTextTrim());
} else if (temp instanceof Map) {
list.add((HashMap) temp);
list.add(iterateElement(et));
} else {
list.add((String) temp);
list.add(et.getTextTrim());
}
obj.put(et.getName(), list);
} else {
obj.put(et.getName(), et.getTextTrim());
}
}
}
return obj;
}
public static void main(String[] args) throws Exception {
String testJson = "{\"head\":{\"opCode\":\"0202\",\"bankCode\":\"04160000\",\"cityCode\":\"370800\"},\"param\":{\"accountNo\":\"A20220101\",\"accountMainNo\":\"A202201\",\"type\":\"0\",\"contractNo\":\"CN20220923050601\",\"tradeAmount\":\"2000000\",\"mode\":\"01\",\"bankSerial\":\"SS2022092317210102\",\"settle\":\"02\",\"outletNo\":\"\",\"tellerNo\":\"\"}}";
System.out.println(testJson);
String toXml = jsonToXml(testJson);
System.out.println(toXml);
String toJson = xmlToJson(toXml);
System.out.println(toJson);
}
}
例外
另外有java XML字符串和bean实体类互转
代码,地址:https://www.cnblogs.com/daen/p/16249882.html