1、例如对象:
{ "id":"111", "strArr":["222","333"], "messList":[{"code":"444","message":"555"},{"code":"666","message":"777"}] }
2、对应的XML
<service> <id>111</id> <array> <strArr>222</strArr> <strArr>333</strArr> </array> <array> <messList> <code>444</code> <message>555</message> </messList> <messList> <code>666</code> <message>777</message> </messList> </array> </service>
3、可能用到的依赖包
<!--json和xml互转所需要的依赖--> <dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>de.odysseus.staxon</groupId> <artifactId>staxon</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.1</version> </dependency>
4、JSON转XML的工具类
import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import java.util.*; public class JsonToXmlUtil { //定义根节点标签 private static final String ROOT_LABEL = "service"; //定义数组、集合的父标签 private static final String ARRAY_ELEMENT = "array"; /** * * @param json json字符串 * @return xml字符串 */ public static String JsonToXml(String json) { Document document = DocumentHelper.createDocument(); Element root = document.addElement(this.ROOT_LABEL);// 默认根节点 Element el = jsonToElement(json, root); String xml = el.asXML(); return xml; } /** * 将json字符串转成Element * * @param json json字符串 * @param parentElement 根节点 * @return */ private static Element jsonToElement(String json, Element parentElement) { JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject(); Element element = jsonElementToElement(jsonObject, parentElement, null); return element; } /** * 将jsonElement转成xml的Element * * @param jsonElement 待解析json对象元素 * @param parentElement 上一层xml的dom对象 * @param name 父节点 * @return */ private static Element jsonElementToElement(JsonElement jsonElement, Element parentElement, String name) { if (jsonElement instanceof JsonArray) { // 如果是集合,需要在外面包一层<array></array>标签 Element newParentElement = parentElement.addElement(this.ARRAY_ELEMENT); JsonArray sonJsonArray = (JsonArray) jsonElement; for (int i = 0; i < sonJsonArray.size(); i++) { JsonElement arrayElement = sonJsonArray.get(i); //递归调用 jsonElementToElement(arrayElement, newParentElement, name); } } else if (jsonElement instanceof JsonObject) { 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) { //递归调用 jsonElementToElement(s.getValue(), currentElement != null ? currentElement : parentElement, s.getKey()); } } else { addAttribute(parentElement, name, jsonElement.getAsString()); } return parentElement; } /** * 设置标签的值 * @param element 父亲节点 * @param name 子节点名字 * @param value 子节点值 */ private static void addAttribute(Element element, String name, String value) { Element element1 = element.addElement(name); element1.addText(value); } }
5、XML转Map
import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import java.util.*; public class XmlToMapUtil { //定义数组、集合的父标签 private static final String ARRAY_ELEMENT = "array"; /** * XML字符串转成MAP * @param xmlString XML字符串 * @return * @throws DocumentException */ public static Map<String, Object> XmlToMap(String xmlString) throws DocumentException { Element rootElement = DocumentHelper.parseText(xmlString).getRootElement(); Map<String, Object> map = parseXmlElementToMap(rootElement); return map; } /** * XML元素转Map * * @param rootElement 根元素Element * @return */ private static Map<String, Object> parseXmlElementToMap(Element rootElement) { Map<String, Object> resultMap = new HashMap<>(); //如果标签是<array>,ARRAY_ELEMENT = "array" if (ARRAY_ELEMENT.equals(rootElement.getName())) { ArrayList<Object> objectArrayList = parseList(rootElement); if(objectArrayList.size()>0){ //如果array标签有子标签,则拿第一个子标签的名字作为集合名字 resultMap.put(rootElement.elements().get(0).getName(), objectArrayList); } } else { Map<String, Object> sonObjectMap = parseObject(rootElement); resultMap.put(rootElement.getName(), sonObjectMap); } return resultMap; } /** * 获取对象map * * @param parentElement 父节点Element * @return */ private static Map<String, Object> parseObject(Element parentElement) { Map<String, Object> resultMap = new HashMap<>(); List<Element> elements = parentElement.elements(); if (elements != null) { for (Element el : elements) { String elName = el.getName(); //如果标签是<array>,ARRAY_ELEMENT = "array" if (ARRAY_ELEMENT.equals(elName)) { ArrayList<Object> elList = parseList(el); resultMap.put(el.elements().get(0).getName(), elList); } else { if (el.elements().size() > 0) { Map<String, Object> objectMap = parseObject(el); resultMap.put(el.getName(), objectMap); } else { resultMap.put(el.getName(), el.getTextTrim()); } } } } return resultMap; } /** * 获取集合 * * @param arrayElement Array标签Element * @return Array标签里的数据集合 */ private static ArrayList<Object> parseList(Element arrayElement) { ArrayList<Object> list = new ArrayList<>(); //如果标签是<array>,ARRAY_ELEMENT = "array" if (ARRAY_ELEMENT.equals(arrayElement.getName())) { List<Element> elements = arrayElement.elements(); if (elements != null) { for (Element el : elements) { if (el.elements().size() > 0) { Map<String, Object> map = parseObject(el); list.add(map); } else { list.add(el.getTextTrim()); } } } } return list; }
根据业务做一些修改。