正在显示
1 个修改的文件
包含
68 行增加
和
51 行删除
| @@ -3,6 +3,8 @@ package com.tianbo.util; | @@ -3,6 +3,8 @@ package com.tianbo.util; | ||
| 3 | import com.alibaba.fastjson.JSONArray; | 3 | import com.alibaba.fastjson.JSONArray; |
| 4 | import com.alibaba.fastjson.JSONObject; | 4 | import com.alibaba.fastjson.JSONObject; |
| 5 | import org.dom4j.Attribute; | 5 | import org.dom4j.Attribute; |
| 6 | +import org.dom4j.Document; | ||
| 7 | +import org.dom4j.DocumentHelper; | ||
| 6 | import org.dom4j.Element; | 8 | import org.dom4j.Element; |
| 7 | 9 | ||
| 8 | import java.util.List; | 10 | import java.util.List; |
| @@ -17,61 +19,76 @@ import static org.apache.commons.lang.StringUtils.isEmpty; | @@ -17,61 +19,76 @@ import static org.apache.commons.lang.StringUtils.isEmpty; | ||
| 17 | public class XmlJson { | 19 | public class XmlJson { |
| 18 | 20 | ||
| 19 | /** | 21 | /** |
| 20 | - * xml转json | ||
| 21 | - * @param element | ||
| 22 | - * @param json | 22 | + * 将xml转换为JSON对象 |
| 23 | + * @param xml xml字符串 | ||
| 24 | + * @return | ||
| 25 | + * @throws Exception | ||
| 23 | */ | 26 | */ |
| 24 | - public static void dom4j2Json(Element element, JSONObject json){ | ||
| 25 | - //如果是属性 | ||
| 26 | - for(Object o:element.attributes()){ | ||
| 27 | - Attribute attr=(Attribute)o; | ||
| 28 | - if(!isEmpty(attr.getValue())){ | ||
| 29 | - json.put("@"+attr.getName(), attr.getValue()); | 27 | + public static JSONObject xmltoJson(String xml) throws Exception { |
| 28 | + JSONObject jsonObject = new JSONObject(); | ||
| 29 | + Document document = DocumentHelper.parseText(xml); | ||
| 30 | + //获取根节点元素对象 | ||
| 31 | + Element root = document.getRootElement(); | ||
| 32 | + iterateNodes(root, jsonObject); | ||
| 33 | + return jsonObject; | ||
| 34 | + } | ||
| 35 | + /** | ||
| 36 | + * 遍历元素 | ||
| 37 | + * @param node 元素 | ||
| 38 | + * @param json 将元素遍历完成之后放的JSON对象 | ||
| 39 | + */ | ||
| 40 | + @SuppressWarnings("unchecked") | ||
| 41 | + public static void iterateNodes(Element node,JSONObject json){ | ||
| 42 | + //获取当前元素的名称 | ||
| 43 | + String nodeName = node.getName(); | ||
| 44 | + //判断已遍历的JSON中是否已经有了该元素的名称 | ||
| 45 | + if(json.containsKey(nodeName)){ | ||
| 46 | + //该元素在同级下有多个 | ||
| 47 | + Object Object = json.get(nodeName); | ||
| 48 | + JSONArray array = null; | ||
| 49 | + if(Object instanceof JSONArray){ | ||
| 50 | + array = (JSONArray) Object; | ||
| 51 | + }else { | ||
| 52 | + array = new JSONArray(); | ||
| 53 | + array.add(Object); | ||
| 54 | + } | ||
| 55 | + //获取该元素下所有子元素 | ||
| 56 | + List<Element> listElement = node.elements(); | ||
| 57 | + if(listElement.isEmpty()){ | ||
| 58 | + //该元素无子元素,获取元素的值 | ||
| 59 | + String nodeValue = node.getTextTrim(); | ||
| 60 | + array.add(nodeValue); | ||
| 61 | + json.put(nodeName, array); | ||
| 62 | + return ; | ||
| 63 | + } | ||
| 64 | + //有子元素 | ||
| 65 | + JSONObject newJson = new JSONObject(); | ||
| 66 | + //遍历所有子元素 | ||
| 67 | + for(Element e:listElement){ | ||
| 68 | + //递归 | ||
| 69 | + iterateNodes(e,newJson); | ||
| 30 | } | 70 | } |
| 71 | + array.add(newJson); | ||
| 72 | + json.put(nodeName, array); | ||
| 73 | + return ; | ||
| 31 | } | 74 | } |
| 32 | - List<Element> chdEl=element.elements(); | ||
| 33 | - if(chdEl.isEmpty()&&!isEmpty(element.getText())){//如果没有子元素,只有一个值 | ||
| 34 | - json.put(element.getName(), element.getText()); | 75 | + //该元素同级下第一次遍历 |
| 76 | + //获取该元素下所有子元素 | ||
| 77 | + List<Element> listElement = node.elements(); | ||
| 78 | + if(listElement.isEmpty()){ | ||
| 79 | + //该元素无子元素,获取元素的值 | ||
| 80 | + String nodeValue = node.getTextTrim(); | ||
| 81 | + json.put(nodeName, nodeValue); | ||
| 82 | + return ; | ||
| 35 | } | 83 | } |
| 36 | - | ||
| 37 | - for(Element e:chdEl){//有子元素 | ||
| 38 | - if(!e.elements().isEmpty()){//子元素也有子元素 | ||
| 39 | - JSONObject chdjson=new JSONObject(); | ||
| 40 | - dom4j2Json(e,chdjson); | ||
| 41 | - Object o=json.get(e.getName()); | ||
| 42 | - if(o!=null){ | ||
| 43 | - JSONArray jsona=null; | ||
| 44 | - if(o instanceof JSONObject){//如果此元素已存在,则转为jsonArray | ||
| 45 | - JSONObject jsono=(JSONObject)o; | ||
| 46 | - json.remove(e.getName()); | ||
| 47 | - jsona=new JSONArray(); | ||
| 48 | - jsona.add(jsono); | ||
| 49 | - jsona.add(chdjson); | ||
| 50 | - } | ||
| 51 | - if(o instanceof JSONArray){ | ||
| 52 | - jsona=(JSONArray)o; | ||
| 53 | - jsona.add(chdjson); | ||
| 54 | - } | ||
| 55 | - json.put(e.getName(), jsona); | ||
| 56 | - }else{ | ||
| 57 | - if(!chdjson.isEmpty()){ | ||
| 58 | - json.put(e.getName(), chdjson); | ||
| 59 | - } | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - }else{//子元素没有子元素 | ||
| 64 | - for(Object o:element.attributes()){ | ||
| 65 | - Attribute attr=(Attribute)o; | ||
| 66 | - if(!isEmpty(attr.getValue())){ | ||
| 67 | - json.put("@"+attr.getName(), attr.getValue()); | ||
| 68 | - } | ||
| 69 | - } | ||
| 70 | - if(!e.getText().isEmpty()){ | ||
| 71 | - json.put(e.getName(), e.getText()); | ||
| 72 | - } | ||
| 73 | - } | 84 | + //有子节点,新建一个JSONObject来存储该节点下子节点的值 |
| 85 | + JSONObject object = new JSONObject(); | ||
| 86 | + //遍历所有一级子节点 | ||
| 87 | + for(Element e:listElement){ | ||
| 88 | + //递归 | ||
| 89 | + iterateNodes(e,object); | ||
| 74 | } | 90 | } |
| 91 | + json.put(nodeName, object); | ||
| 92 | + return ; | ||
| 75 | } | 93 | } |
| 76 | - | ||
| 77 | } | 94 | } |
-
请 注册 或 登录 后发表评论