JSON对象与XML相互转换⼯具类
依赖jar
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
JsonXmlUtils.java
package javax.utils;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.URL;
import java.nio.file.Paths;
import org.apachemons.io.FileUtils;
import org.apachemons.io.IOUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
l.sax.SAXException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
/**
* JSON对象与XML相互转换⼯具类
*
* @author Logan
* @createDate 2019-02-12
* @version 1.0.0
*
*/
public class JsonXmlUtils {
private static final String ENCODING = "UTF-8";
/**
* JSON对象转漂亮的xml字符串
*
* @param json JSON对象
* @return漂亮的xml字符串
* @throws IOException
* @throws SAXException
*/
public static String jsonToPrettyXml(JSONObject json) throws IOException, SAXException {
Document document = jsonToDocument(json);
/* 格式化xml */
OutputFormat format = atePrettyPrint();
// 设置缩进为4个空格
format.setIndent(" ");
format.setIndentSize(4);
StringWriter formatXml = new StringWriter();
XMLWriter writer = new XMLWriter(formatXml, format);
writer.write(document);
String();
}
/**
* JSON对象转xml字符串
*
* @param json JSON对象
* @return xml字符串
* @throws SAXException
*/
public static String JsonToXml(JSONObject json) throws SAXException {
return jsonToDocument(json).asXML();
}
/
**
* JSON对象转Document对象
*
* @param json JSON对象
* @return Document对象
* @throws SAXException
*/
public static Document jsonToDocument(JSONObject json) throws SAXException {        Document document = ateDocument();
document.setXMLEncoding(ENCODING);
// root对象只能有⼀个
for (String rootKey : json.keySet()) {
Element root = JSONObject(rootKey), rootKey);
document.add(root);
break;
}
return document;
}
/**
* JSON对象转Element对象
*
* @param json JSON对象
* @param nodeName 节点名称
* @return Element对象
*/
public static Element jsonToElement(JSONObject json, String nodeName) {
Element node = ateElement(nodeName);
for (String key : json.keySet()) {
Object child = (key);
if (child instanceof JSONObject) {
node.add(JSONObject(key), key));
}
else {
Element element = ateElement(key);
element.String(key));
node.add(element);
}
}
return node;
}
/**
* XML字符串转JSON对象
*
* @param xml xml字符串
* @return JSON对象
* @throws DocumentException
*/
public static JSONObject xmlToJson(String xml) throws DocumentException {
JSONObject json = new JSONObject();
SAXReader reader = new SAXReader();
Document document = ad(new StringReader(xml));
Element root = RootElement();
json.Name(), elementToJson(root));
return json;
}
/**
* Element对象转JSON对象
*
* @param element Element对象
* @return JSON对象
*/
public static JSONObject elementToJson(Element element) {
JSONObject json = new JSONObject();
for (Object child : element.elements()) {
Element e = (Element) child;
if (e.elements().isEmpty()) {
json.Name(), e.getText());
}
else {
json.Name(), elementToJson(e));
}
}
return json;
}
/**json转换对象
* ⽂件内容转换成字符串
*
* @param filePath ⽂件路径
* @return内容字符串
* @throws IOException
*/
public static String fileToString(URL filePath) throws IOException {
String(filePath, ENCODING);
}
/**
* ⽂件内容转换成字符串
*
* @param filePath ⽂件路径
* @return内容字符串
* @throws IOException
*/
public static String fileToString(String filePath) throws IOException {
(filePath).toUri(), ENCODING);
}
/**
* 字符串输出到⽂件
*
* @param str 字符串内容
* @param filePath ⽂件路径
* @throws IOException
*/
public static void stringToFile(String str, String filePath) throws IOException {        FileUtils.(filePath).toFile(), str, ENCODING);
}
/**
* 字符串输出到⽂件
*
* @param str 字符串内容
* @param filePath ⽂件路径
* @throws IOException
*/
public static void stringToFile(String str, URL filePath) throws IOException {        FileUtils.writeStringToFile(new Path()), str, ENCODING);    }
/**
* 字符串输出到⽂件
*
* @param str 字符串内容
* @param file 输出⽂件
* @throws IOException
*/
public static void stringToFile(String str, File file) throws IOException {
FileUtils.writeStringToFile(file, str, ENCODING);
}
public static void main(String[] args) {
try {
String filePath = "/l";
URL url = Resource(filePath);
String content = JsonXmlUtils.fileToString(url);
// System.out.println(content);
JSONObject json = xmlToJson(content);
System.out.JSONString(json, true));
String xml = JsonToXml(json);
System.out.println(xml);
System.out.println("----------------------------------------\n\n");            xml = jsonToPrettyXml(json);
System.out.println(xml);
stringToFile(xml, "G:\\Temp\\Test\\l");
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}
测试⽂件
<?xml version="1.0" encoding="UTF-8"?>
<entity>
<user>
<id>1001</id>
<username>Logan</username>
<password>666666</password>
<age>16</age>
</user>
<order>
<id>2001</id>
<price>9.99</price>
<date>2019-02-12</date>
</order>
</entity>
.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。