当前位置: 代码迷 >> 综合 >> json转xml(JSONObject内部包含JSONArray)
  详细解决方案

json转xml(JSONObject内部包含JSONArray)

热度:24   发布时间:2023-09-28 00:41:35.0

找了很多网上的资料,要么只能转jsonobject要么只能转jsonarray,下面是两个混合能转的工具类 

<dependency><groupId>org.dom4j</groupId><artifactId>dom4j</artifactId><version>2.1.1</version>
</dependency>

代码部分 

package com.xingyi.rscmgr.utils.conversion;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.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;
import org.xml.sax.SAXException;import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;
import java.nio.file.Paths;/*** @author dongxingli* Created on 2019/4/3 19:00*/
public class JsonXmlUtils {private static final String ENCODING = "UTF-8";/*** JSON对象转的xml字符串-格式化 带空格 换行符 输出到文件可读性好** @param json JSON对象* @return 漂亮的xml字符串*/public static String jsonToFormatXml(JSONObject json) {StringWriter formatXml = new StringWriter();try {Document document = jsonToDocument(json);/* 格式化xml */OutputFormat format = OutputFormat.createPrettyPrint();// 设置缩进为4个空格format.setIndent(" ");format.setIndentSize(4);XMLWriter writer = new XMLWriter(formatXml, format);writer.write(document);} catch (IOException e) {e.printStackTrace();}return formatXml.toString();}/*** JSON对象转xml字符串-不带空格换行符** @param json JSON对象* @return xml字符串* @throws SAXException*/public static String JsonToXml(JSONObject json) {return jsonToDocument(json).asXML();}/*** JSON对象转Document对象** @param json JSON对象* @return Document对象*/public static Document jsonToDocument(JSONObject json) {Document document = DocumentHelper.createDocument();document.setXMLEncoding(ENCODING);for (String rootKey : json.keySet()) {Element root = DocumentHelper.createElement(rootKey);addJsonObjectElement(json.getJSONObject(rootKey), root);document.add(root);// root对象只能有一个,多个时只读一个break;}return document;}/*** 添加JSONArray Element** @param jsonArray 传入的json数组* @param nodeName  节点名称* @param arrRoot   上层节点*/private static void addJsonArrayElement(JSONArray jsonArray, String nodeName, Element arrRoot) {for (Object aJsonArray : jsonArray) {JSONObject jsonObject = (JSONObject) aJsonArray;Element node = DocumentHelper.createElement(nodeName);// 继续遍历for (String key : jsonObject.keySet()) {Element element = DocumentHelper.createElement(key);Object child = JSON.toJSON(jsonObject.get(key));if (child instanceof JSONArray) {// 递归addJsonArrayElement(jsonObject.getJSONArray(key), key, node);} else if (child instanceof JSONObject) {addJsonObjectElement(jsonObject.getJSONObject(key), element);node.add(element);} else {element.setText(jsonObject.getString(key));node.add(element);}}arrRoot.add(node);}}/*** 添加JSONObject Element** @param json JSON对象* @param root 上层节点* @return Element对象*/private static void addJsonObjectElement(JSONObject json, Element root) {for (String key : json.keySet()) {Element node = DocumentHelper.createElement(key);Object child = json.get(key);if (child instanceof JSONObject) {addJsonObjectElement(json.getJSONObject(key), node);root.add(node);} else if (child instanceof JSONArray) {addJsonArrayElement(json.getJSONArray(key), key, root);} else {node.setText(json.getString(key));root.add(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 = reader.read(new StringReader(xml));Element root = document.getRootElement();json.put(root.getName(), 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.put(e.getName(), e.getText());} else {json.put(e.getName(), elementToJson(e));}}return json;}/*** 文件内容转换成字符串** @param filePath 文件路径* @return 内容字符串* @throws IOException*/public static String fileToString(URL filePath) throws IOException {return IOUtils.toString(filePath, ENCODING);}/*** 文件内容转换成字符串** @param filePath 文件路径* @return 内容字符串* @throws IOException*/public static String fileToString(String filePath) throws IOException {return IOUtils.toString(Paths.get(filePath).toUri(), ENCODING);}/*** 字符串输出到文件** @param str      字符串内容* @param filePath 文件路径* @throws IOException*/public static void stringToFile(String str, String filePath) throws IOException {FileUtils.writeStringToFile(Paths.get(filePath).toFile(), str, ENCODING);}/*** 字符串输出到文件** @param str      字符串内容* @param filePath 文件路径* @throws IOException*/public static void stringToFile(String str, URL filePath) throws IOException {FileUtils.writeStringToFile(new File(filePath.getPath()), str, ENCODING);}
}

 

  相关解决方案