XML转JSON
相信你和我一样找过很多的XML转JSON的方法,但是不管哪种一定都会面临迭代问题,属性值和标签值的安放问题,同名标签组织问题。我通过网上找了一通,测试后才发现还是得自己写,实在不忍他人也如是,特此Mark一下。XML的Reader随意只是我选了dom4j。
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.util.List;
import java.util.Map;
/**
* @author zm
* @date 2020/3/11 0011 21:06
*/
public class Xml2Json {
public static void main(String[] args) throws Exception {
String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<message>\n" +
" <header>\n" +
" <info messageClass=\"1000\" transferTime=\"20200102075957\">infoTest</info>\n" +
" <location machineID=\"2EEEEF01\" groupName=\"REFS\" laneNo=\"1\">locationTest</location>\n" +
" <location machineID=\"2AAAEF02\" groupName=\"REFS\" laneNo=\"2\">locationTest2</location>\n" +
" </header>\n" +
" <body>\n" +
" <pcb id=\"13000000\"></pcb>\n" +
" </body>\n" +
"</message>";
JSONObject json = xml2Json(xmlStr);
System.out.println(json);
}
public static JSONObject xml2Json(String xmlStr) {
if (xmlStr == null) {
return null;
} else {
xmlStr = xmlStr.trim();
}
JSONObject json = new JSONObject();
try {
Document doc = DocumentHelper.parseText(xmlStr);
Element root = doc.getRootElement();
json.put(root.getName(), dom4j2Json(doc.getRootElement(), json));
} catch (Exception ex) {
json.put("error", ex.getMessage());
}
return json;
}
/**
* xml转json
*
* @param element
* @param parent
*/
public static JSONObject dom4j2Json(Element element, JSONObject parent) {
JSONObject json = new JSONObject();
//如果是属性
for (Object o : element.attributes()) {
Attribute attr = (Attribute) o;
json.put(attr.getName(), attr.getValue());
}
String txt = element.getText();
if (txt != null && !txt.trim().isEmpty()) {
json.put("_data", txt);
}
List<Element> chdEl = element.elements();
for (Element e : chdEl) {
JSONObject chdjson = dom4j2Json(e, json);
Object o = json.get(e.getName());
if (o != null) {
JSONArray jsonArr = null;
if (o instanceof JSONObject) {
//如果此元素已存在,则转为jsonArray
JSONObject jsono = (JSONObject) o;
json.remove(e.getName());
jsonArr = new JSONArray();
jsonArr.add(jsono);
jsonArr.add(chdjson);
}
if (o instanceof JSONArray) {
jsonArr = (JSONArray) o;
jsonArr.add(chdjson);
}
json.put(e.getName(), jsonArr);
} else {
if (!chdjson.isEmpty()) {
json.put(e.getName(), chdjson);
}
}
}
return json;
}
}
输出结果:
{
"message": {
"header": {
"location": [{
"_data": "locationTest",
"groupName": "REFS",
"machineID": "2EEEEF01",
"laneNo": "1"
},
{
"_data": "locationTest2",
"groupName": "REFS",
"machineID": "2AAAEF02",
"laneNo": "2"
}],
"info": {
"_data": "infoTest",
"transferTime": "20200102075957",
"messageClass": "1000"
}
},
"body": {
"pcb": {
"id": "13000000"
}
}
}
}