架包引用:
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
代码:
import net.sf.json.JSONSerializer;
import net.sf.json.xml.XMLSerializer;
import java.util.Map;
/**
* Copyright (C), 2011-2018 {company}
* FileName: com.mina.utils.XmlJsonUtils.java
* Author: xxx
* Email: xxx
* Date: 2018/6/15 23:36
* Description:
* History:
* <Author> <Time> <version> <desc>
* {xxx} 23:36 1.0 Create
*/
public class XmlJsonUtils {
/**
* JSON(数组)字符串转换成XML字符串
* (必须引入 xom-1.1.jar)
* @param jsonString
* @return
*/
public static String json2xml(String jsonString) {
XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setTypeHintsEnabled(false); // 去除 节点中type类型
String xml = xmlSerializer.write(JSONSerializer.toJSON(jsonString));
xml = xml.replace("<o>", "").replace("</o>", "");
xml = xml.replaceAll("\r\n", "").concat("\r\n");
return xml;
}
/**
* xml 转 json
* @param xmlString xml字符串
* @return
*/
public static String xml2json(String xmlString) {
Map<String, Object> map = XmlMapUtils.xmlToMap(xmlString);
String json = JsonToMap.mapToJson(map);
return json;
}
}