package com.jttech.pfcs.util;
|
|
import com.alibaba.fastjson.JSONObject;
|
import org.jdom2.Document;
|
import org.jdom2.Element;
|
import org.jdom2.input.SAXBuilder;
|
import org.springframework.util.StringUtils;
|
|
|
import java.io.ByteArrayInputStream;
|
import java.io.InputStream;
|
import java.util.LinkedList;
|
import java.util.List;
|
|
/**
|
* @author Skaði the Corrupting Heart
|
* @version 1.0.0
|
* @ClassName xml.java
|
* @Description TODO
|
* @createTime 2022年04月02日 17:12
|
*/
|
public class XmlUtils {
|
|
public static JSONObject xml2Json(String xmlStr) {
|
try {
|
if (StringUtils.isEmpty(xmlStr)) {
|
return null;
|
}
|
xmlStr = xmlStr.replaceAll("\\\n", "");
|
byte[] xml = xmlStr.getBytes("UTF-8");
|
JSONObject json = new JSONObject();
|
InputStream is = new ByteArrayInputStream(xml);
|
SAXBuilder sb = new SAXBuilder();
|
Document doc = sb.build(is);
|
Element root = doc.getRootElement();
|
json.put(root.getName(), iterateElement(root));
|
|
return json;
|
} catch (Exception pE) {
|
throw new RuntimeException("xml文件解析失败", pE);
|
}
|
|
|
}
|
|
|
private static JSONObject iterateElement(Element element) {
|
List<Element> node = element.getChildren();
|
JSONObject obj = new JSONObject();
|
List list = null;
|
for (Element child : node) {
|
list = new LinkedList();
|
String text = child.getTextTrim();
|
if (StringUtils.isEmpty(text)) {
|
if (child.getChildren().size() == 0) {
|
continue;
|
}
|
if (obj.containsKey(child.getName())) {
|
list = (List) obj.get(child.getName());
|
}
|
list.add(iterateElement(child)); // 遍历child的子节点
|
obj.put(child.getName(), list);
|
} else {
|
if (obj.containsKey(child.getName())) {
|
Object value = obj.get(child.getName());
|
try {
|
list = (List) value;
|
} catch (ClassCastException e) {
|
list.add(value);
|
}
|
}
|
if (child.getChildren().size() == 0) { // child无子节点时直接设置text
|
obj.put(child.getName(), text);
|
} else {
|
list.add(text);
|
obj.put(child.getName(), list);
|
}
|
}
|
}
|
return obj;
|
|
}
|
}
|