package com.yx.emr.base.common;
import java.util.HashMap;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class XmlToJson {
/**
* 将xml转换为JSON对象
*
* @param xml xml字符串
* @return
* @throws Exception
*/
public static JSONObject xmltoJson(String xml) throws Exception {
JSONObject jsonObject = new JSONObject();
Document document = DocumentHelper.parseText(xml);
// 获取根节点元素对象
Element root = document.getRootElement();
iterateNodes(root, jsonObject);
return jsonObject;
}
/**
* 遍历元素
*
* @param node 元素
* @param json 将元素遍历完成之后放的JSON对象
*/
@SuppressWarnings("unchecked")
public static void iterateNodes(Element node, JSONObject json) {
// 获取当前元素的名称
String nodeName = node.getName();
// 判断已遍历的JSON中是否已经有了该元素的名称
if (json.containsKey(nodeName)) {
// 该元素在同级下有多个
Object Object = json.get(nodeName);
JSONArray array = null;
if (Object instanceof JSONArray) {
array = (JSONArray) Object;
} else {
array = new JSONArray();
array.add(Object);
}
// 获取该元素下所有子元素
List<Element> listElement = node.elements();
if (listElement.isEmpty()) {
// 该元素无子元素,获取元素的值
String nodeValue = node.getTextTrim();
array.add(nodeValue);
json.put(nodeName, array);
return;
}
// 有子元素
JSONObject newJson = new JSONObject();
// 遍历所有子元素
for (Element e : listElement) {
// 递归
iterateNodes(e, newJson);
}
array.add(newJson);
json.put(nodeName, array);
return;
}
// 该元素同级下第一次遍历
// 获取该元素下所有子元素
List<Element> listElement = node.elements();
if (listElement.isEmpty()) {
// 该元素无子元素,获取元素的值
String nodeValue = node.getTextTrim();
json.put(nodeName, nodeValue);
return;
}
// 有子节点,新建一个JSONObject来存储该节点下子节点的值
JSONObject object = new JSONObject();
// 遍历所有一级子节点
for (Element e : listElement) {
// 递归
iterateNodes(e, object);
}
json.put(nodeName, object);
return;
}
public static JSONObject xmltoJsonData(String xml, HashMap<String,String> arrayNodeMap) throws Exception {
JSONObject jsonObject = xmltoJson(xml);
JSONObject bodyObject = jsonObject.getJSONObject("zhyx").getJSONObject("body");
if (arrayNodeMap!=null) {
arrayNodeMap.forEach((nodekey,nodeVal)->{
Object keyObj= bodyObject.get(nodekey);
if (keyObj instanceof JSONObject){
Object childObj = ((JSONObject) keyObj).get(nodeVal);
JSONArray jsonArr = null;
if (childObj instanceof JSONObject){
jsonArr = new JSONArray();
jsonArr.add(childObj);
}else if(childObj instanceof JSONArray){
jsonArr = (JSONArray)childObj;
}
bodyObject.remove(nodekey);
bodyObject.put(nodekey,jsonArr);
}
});
}
return bodyObject;
}
/**
* 测试的main方法
*/
public static void main(String[] args) throws Exception {
String xml ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+"<zhyx>"
+"<header>"
+"<appkey> </appkey>"
+"<user> </user>"
+"<timestamp> </timestamp>"
+" </header>"
+"<body>"
+"<emrInstructInfo>"
+"<age>6</age>"
+"<ageUnit>1</ageUnit>"
+"<allergicHistory></allergicHistory>"
+"<applyDoctor>刘**</applyDoctor>"
+"<applyTime>2018-04-18 17:16:53</applyTime>"
+"<checkTime>2018-04-18 18:16:53</checkTime>"
+"<department>外科</department>"
+"<diseaseName>急性肾衰竭</diseaseName>"
+"<icdCode>ICD-10</icdCode>"
+"<idCard>450881199412087728</idCard>"
+"<inhospitalCode></inhospitalCode>"
+"<inhospitalNo></inhospitalNo>"
+"<inpatientTime></inpatientTime>"
+"<medicareNo>6680680010</medicareNo>"
+"<name>张三丰</name>"
+"<outTime></outTime>"
+"<outpatientNo>100068</outpatientNo>"
+"<patientId>0755123456798</patientId>"
+"<previousHistory></previousHistory>"
+"<sex>2</sex>"
+"<temperature></temperature>"
+"<pulse></pulse>"
+"<breath></breath>"
+"<bloodPressure></bloodPressure>"
+"</emrInstructInfo>"
+"<diseaseList>"
+"<diseaseItem>"
+"<diseaseCode>11</diseaseCode>"
+"<diseaseName>1111</diseaseName>"
+"</diseaseItem>"
+"<diseaseItem>"
+"<diseaseCode>22</diseaseCode>"
+"<diseaseName>2222</diseaseName>"
+"</diseaseItem>"
+"</diseaseList>"
+"<adviceList>"
+"<advice>"
+"<adviceName>透析</adviceName>"
+"<adviceNo></adviceNo>"
+"<startTime></startTime>"
+"<adviceType>治疗</adviceType>"
+"<totalNum></totalNum>"
+"<dose></dose>"
+"<unit></unit>"
+"<frequency></frequency>"
+"<usage></usage>"
+"</advice>"
+"</adviceList>"
+"</body>"
+"</zhyx>";
HashMap<String,String> arrayNodeMap = new HashMap<>();
arrayNodeMap.put("diseaseList","diseaseItem");
arrayNodeMap.put("adviceList","advice");
JSONObject bodyObject = xmltoJsonData(xml,arrayNodeMap);
// System.out.println(bodyObject);
String httpParam = JsonUtils.getJsonString(bodyObject);
System.out.println(httpParam);
}
}