java中 xml转json 或者 json转xml工具类
首先引入依赖
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<classifier>jdk15</classifier>
<version>2.4</version>
</dependency>
<dependency>
<groupId>xom</groupId>
<artifactId>xom</artifactId>
<version>1.2.5</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20171018</version>
</dependency>
XmlJsonUtils.java
import net.sf.json.JSONSerializer;
import net.sf.json.xml.XMLSerializer;
import java.util.Map;
public class XmlJsonUtils {
/**
* JSON(数组)字符串转换成XML字符串
*/
public static String json2xml(String jsonString) {
XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setTypeHintsEnabled(false);
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
*/
public static String xml2json(String xmlString) {
JSONObject jsonObject = XML.toJSONObject(xmlString);
return jsonObject.toString(3);
}
}