Xml2JsonUtils.java 4.56 KB
package com.sincere.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.*;

import java.util.List;

/**
 * @author chen
 * @version 1.0
 * @date 2019/12/26 0026 10:19
 */
public class Xml2JsonUtils {


    public static void main(String[] args) throws Exception {
        String xmlStr=
                "<JXT Name=\"TeacherInfo\">" +
                "<resultInfo><userID>9943601</userID><name>abd</name><classID>05750400</classID><account>13388685716</account><cardID></cardID><cardID2></cardID2><cardID3></cardID3><cardID4></cardID4><cardID5></cardID5><career>校管理员</career><familyPhone1></familyPhone1><familyPhone2>0</familyPhone2><familyPhone3>0</familyPhone3><tempString></tempString><tempString2></tempString2><tempString3></tempString3></resultInfo>" +
                "<resultInfo><userID>18639924</userID><name>计老师</name><classID>05750400-D000001</classID><account>15757371362</account><cardID></cardID><cardID2></cardID2><cardID3></cardID3><cardID4></cardID4><cardID5></cardID5><career>教师</career><familyPhone1></familyPhone1><familyPhone2>0</familyPhone2><familyPhone3>0</familyPhone3><tempString></tempString><tempString2></tempString2><tempString3></tempString3></resultInfo>" +
                "<resultInfo><userID>30962787</userID><name>王佳奕1</name><classID>05750400-D000001</classID><account>13000000000</account><cardID></cardID><cardID2></cardID2><cardID3></cardID3><cardID4></cardID4><cardID5></cardID5><career>教师</career><familyPhone1></familyPhone1><familyPhone2>0</familyPhone2><familyPhone3>0</familyPhone3><tempString></tempString><tempString2></tempString2><tempString3></tempString3></resultInfo>" +
                "</JXT>";
        Document doc= DocumentHelper.parseText(xmlStr);
        JSONObject json=new JSONObject();
        dom4j2Json(doc.getRootElement(),json);
        System.out.println("xml2Json:"+json.toJSONString());
    }

    /**
     * xml转json
     * @param xmlStr
     * @return
     * @throws DocumentException
     */
    public static JSONObject xml2Json(String xmlStr){
        try{
            Document doc= DocumentHelper.parseText(xmlStr);
            JSONObject json=new JSONObject();
            dom4j2Json(doc.getRootElement(), json);
            return json;
        }catch (Exception e){

        }
        return null ;
    }

    /**
     * xml转json
     * @param element
     * @param json
     */
    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());
            }
        }
        List<Element> chdEl=element.elements();
        if(chdEl.isEmpty()&&!isEmpty(element.getText())){//如果没有子元素,只有一个值
            json.put(element.getName(), element.getText());
        }

        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());
                }
            }
        }
    }

    public static boolean isEmpty(String str) {

        if (str == null || str.trim().isEmpty() || "null".equals(str)) {
            return true;
        }
        return false;
    }
}