...
|
...
|
@@ -3,6 +3,8 @@ package com.tianbo.util; |
|
|
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 java.util.List;
|
...
|
...
|
@@ -17,61 +19,76 @@ import static org.apache.commons.lang.StringUtils.isEmpty; |
|
|
public class XmlJson {
|
|
|
|
|
|
/**
|
|
|
* xml转json
|
|
|
* @param element
|
|
|
* @param json
|
|
|
* 将xml转换为JSON对象
|
|
|
* @param xml xml字符串
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public static void dom4j2Json(Element element, JSONObject json){
|
|
|
//如果是属性
|
|
|
for(Object o:element.attributes()){
|
|
|
Attribute attr=(Attribute)o;
|
|
|
if(!isEmpty(attr.getValue())){
|
|
|
json.put("@"+attr.getName(), attr.getValue());
|
|
|
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> chdEl=element.elements();
|
|
|
if(chdEl.isEmpty()&&!isEmpty(element.getText())){//如果没有子元素,只有一个值
|
|
|
json.put(element.getName(), element.getText());
|
|
|
//该元素同级下第一次遍历
|
|
|
//获取该元素下所有子元素
|
|
|
List<Element> listElement = node.elements();
|
|
|
if(listElement.isEmpty()){
|
|
|
//该元素无子元素,获取元素的值
|
|
|
String nodeValue = node.getTextTrim();
|
|
|
json.put(nodeName, nodeValue);
|
|
|
return ;
|
|
|
}
|
|
|
|
|
|
for(Element e:chdEl){//有子元素
|
|
|
if(!e.elements().isEmpty()){//子元素也有子元素
|
|
|
JSONObject chdjson=new JSONObject();
|
|
|
dom4j2Json(e,chdjson);
|
|
|
Object o=json.get(e.getName());
|
|
|
if(o!=null){
|
|
|
JSONArray jsona=null;
|
|
|
if(o instanceof JSONObject){//如果此元素已存在,则转为jsonArray
|
|
|
JSONObject jsono=(JSONObject)o;
|
|
|
json.remove(e.getName());
|
|
|
jsona=new JSONArray();
|
|
|
jsona.add(jsono);
|
|
|
jsona.add(chdjson);
|
|
|
}
|
|
|
if(o instanceof JSONArray){
|
|
|
jsona=(JSONArray)o;
|
|
|
jsona.add(chdjson);
|
|
|
}
|
|
|
json.put(e.getName(), jsona);
|
|
|
}else{
|
|
|
if(!chdjson.isEmpty()){
|
|
|
json.put(e.getName(), chdjson);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
}else{//子元素没有子元素
|
|
|
for(Object o:element.attributes()){
|
|
|
Attribute attr=(Attribute)o;
|
|
|
if(!isEmpty(attr.getValue())){
|
|
|
json.put("@"+attr.getName(), attr.getValue());
|
|
|
}
|
|
|
}
|
|
|
if(!e.getText().isEmpty()){
|
|
|
json.put(e.getName(), e.getText());
|
|
|
}
|
|
|
}
|
|
|
//有子节点,新建一个JSONObject来存储该节点下子节点的值
|
|
|
JSONObject object = new JSONObject();
|
|
|
//遍历所有一级子节点
|
|
|
for(Element e:listElement){
|
|
|
//递归
|
|
|
iterateNodes(e,object);
|
|
|
}
|
|
|
json.put(nodeName, object);
|
|
|
return ;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|