import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.*;
import java.util.List;
import java.util.Set;
public class XmlConvert {
private String attributeNamePrefix;
private String textKey;
private boolean removeHeader;
public XmlConvert() {
this.setAttributeNamePrefix("@");
this.setTextKey("#text");
this.setRemoveHeader(true);
}
public XmlConvert(String attributeNamePrefix, String textKey) {
this(attributeNamePrefix, textKey, true);
}
public XmlConvert(String attributeNamePrefix, String textKey, boolean removeHeader) {
this.attributeNamePrefix = attributeNamePrefix;
this.textKey = textKey;
this.removeHeader = removeHeader;
}
public String jsonToXml(String json) {
JSONObject jsonObject = JSON.parseObject(json);
Set<String> keySet = jsonObject.keySet();
if (!keySet.isEmpty() && keySet.size() == 1) {
String rootName = keySet.iterator().next();
Element rootElement = DocumentHelper.createElement(rootName);
Element element = processObject(jsonObject.get(rootName), rootElement);
String xml = DocumentHelper.createDocument(element).asXML();
if (isRemoveHeader()) {
xml = removeHeader(xml);
}
return xml;
} else {
throw new RuntimeException("The json text is not formatted correctly");
}
}
private Element processObject(Object object, Element element) {
if (object instanceof JSONObject) {
return processJSONObject((JSONObject) object, element);
} else if (object instanceof JSONArray) {
return processJSONArray((JSONArray) object, element, element.getName());
} else {
return processText(object.toString(), element);
}
}
private static Element processText(String text, Element element) {
element.setText(text);
return element;
}
private Element processJSONObject(JSONObject jsonObject, Element element) {
jsonObject.forEach((key, value) -> {
if (key.startsWith(getAttributeNamePrefix())) {
element.addAttribute(key.substring(getPrefixLength()), value.toString());
} else if (key.equals(getTextKey())) {
element.setText(value.toString());
} else {
processValue(element, key, value);
}
});
return element;
}
private void processValue(Element element, String name, Object value) {
if (value instanceof JSONObject) {
Element tempElement = processJSONObject((JSONObject) value, DocumentHelper.createElement(name));
element.add(tempElement);
} else if (value instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) value;
int size = jsonArray.size();
for (int i = 0; i < size; i++) {
processValue(element, name, jsonArray.get(i));
}
} else {
Element temp = processText(value.toString(), DocumentHelper.createElement(name));
element.add(temp);
}
}
private Element processJSONArray(JSONArray jsonArray, Element root, String name) {
int size = jsonArray.size();
for (int i = 0; i < size; ++i) {
processValue(root, name, jsonArray.get(i));
}
return root;
}
public String xmlToJson(String xml) {
try {
Document document = DocumentHelper.parseText(xml);
Element rootElement = document.getRootElement();
JSON json = processObjectElement(rootElement);
JSONObject jsonObject = new JSONObject();
jsonObject.put(rootElement.getName(), json);
return jsonObject.toJSONString();
} catch (DocumentException e) {
throw new RuntimeException("The XML text is not formatted correctly", e);
}
}
private JSONObject processObjectElement(Element element) {
if (element == null) {
return new JSONObject();
}
JSONObject jsonObject = new JSONObject();
List<Attribute> attributes = element.attributes();
for (Attribute attribute : attributes) {
String attributeName = getAttributeNamePrefix() + attribute.getName();
String attributeValue = attribute.getValue();
setOrAccumulate(jsonObject, attributeName, attributeValue);
}
int nodeCount = element.nodeCount();
for (int i = 0; i < nodeCount; i++) {
Node node = element.node(i);
if (node instanceof Text) {
Text text = (Text) node;
setOrAccumulate(jsonObject, getTextKey(), text.getText());
} else if (node instanceof Element) {
setValue(jsonObject, (Element) node);
}
}
return jsonObject;
}
private void setValue(JSONObject jsonObject, Element element) {
if (isObject(element)) {
JSONObject elementJsonObj = processObjectElement(element);
setOrAccumulate(jsonObject, element.getName(), elementJsonObj);
} else {
setOrAccumulate(jsonObject, element.getName(), element.getStringValue());
}
}
private boolean isObject(Element element) {
int attributeCount = element.attributeCount();
if (attributeCount > 0) {
return true;
}
int attrs = element.nodeCount();
if (attrs == 1 && element.node(0) instanceof Text) {
return false;
} else {
return true;
}
}
private void setOrAccumulate(JSONObject jsonObject, String key, Object value) {
if (jsonObject.containsKey(key)) {
Object obj = jsonObject.get(key);
if (obj instanceof JSONArray) {
((JSONArray) obj).add(value);
} else {
JSONArray jsonArray = new JSONArray();
jsonArray.add(obj);
jsonArray.add(value);
jsonObject.put(key, jsonArray);
}
} else {
jsonObject.put(key, value);
}
}
private String removeHeader(String source) {
return source.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "")
.replaceAll("\r|\n", "");
}
public String getAttributeNamePrefix() {
return attributeNamePrefix;
}
public void setAttributeNamePrefix(String attributeNamePrefix) {
this.attributeNamePrefix = attributeNamePrefix;
}
public String getTextKey() {
return textKey;
}
public void setTextKey(String textKey) {
this.textKey = textKey;
}
public boolean isRemoveHeader() {
return removeHeader;
}
public void setRemoveHeader(boolean removeHeader) {
this.removeHeader = removeHeader;
}
private int getPrefixLength() {
return this.attributeNamePrefix.length();
}
}
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.xml.XMLSerializer;
public class XmlJsonUtils {
private XmlJsonUtils() {
}
@Deprecated
public static String xmlToJsonStr(String xml) {
return xmlToJsonStr(xml, false);
}
@Deprecated
public static String xmlToJsonStr(String xml, boolean forceTopLevelObject) {
XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setForceTopLevelObject(forceTopLevelObject);
return xmlSerializer.read(xml).toString();
}
@Deprecated
public static String jsonToXml(String jsonStr) {
return jsonToXml(jsonStr, "");
}
@Deprecated
public static String jsonToXml(String jsonStr, String rootName) {
return jsonToXml(jsonStr, rootName, null);
}
@Deprecated
public static String jsonToXml(String jsonStr, String rootName, String elementName) {
XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setTypeHintsEnabled(false);
xmlSerializer.setRootName(rootName);
xmlSerializer.setElementName(elementName);
JSON json = getJSON(jsonStr);
String xmlStr = xmlSerializer.write(json);
return removeHeaderMessage(xmlStr);
}
@Deprecated
private static String removeHeaderMessage(String source) {
return source.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "").replaceAll("\r|\n", "");
}
@Deprecated
private static JSON getJSON(String jsonStr) {
return JSONSerializer.toJSON(jsonStr);
}
public static String xmlToJSON(String xml, String attributeNamePrefix, String textKey) {
XmlConvert xmlConvert = new XmlConvert(attributeNamePrefix, textKey);
return xmlConvert.xmlToJson(xml);
}
public static String xmlToJSON(String xml, String attributeNamePrefix) {
return xmlToJSON(xml, attributeNamePrefix, "#text");
}
public static String xmlToJSON(String xml) {
return xmlToJSON(xml, "@");
}
public static String jsonToXML(String json, String attributeNamePrefix, String textKey, boolean removeHeader) {
XmlConvert xmlConvert = new XmlConvert(attributeNamePrefix, textKey, removeHeader);
return xmlConvert.jsonToXml(json);
}
public static String jsonToXML(String json, String attributeNamePrefix, String textKey) {
return jsonToXML(json, attributeNamePrefix, textKey, true);
}
public static String jsonToXML(String json, String attributeNamePrefix) {
return jsonToXML(json, attributeNamePrefix, "#text", true);
}
public static String jsonToXML(String json) {
return jsonToXML(json, "@");
}
}
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>