zoukankan      html  css  js  c++  java
  • XML 转 JSON

    package com.Text;
    import java.util.List;
    import org.dom4j.Attribute;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import com.alibaba.fastjson.JSONObject;
    import net.sf.json.JSONArray;
    
    public class xmlJson {
    
        /**
         * 获得xml
         *
         * @param xmlStr
         * @return
         * @throws DocumentException
         */
        public static JSONObject xml(String xmlStr) throws DocumentException {
            Document doc = DocumentHelper.parseText(xmlStr);
            JSONObject json = new JSONObject();
            xmlToJson(doc.getRootElement(), json);
            return json;
        }
     
        /**
         * xml转json
         *
         * @param element
         * @param json
         */
        public static void xmlToJson(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();
                    xmlToJson(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;
        }
        
        
        
        
        
    }
  • 相关阅读:
    [记读书笔]python3.5实现socket通讯(UDP)
    [安全学习环境]Win7 下DVWA安装指南
    [Android]Android SDk Manager中创建模拟器无法选择CPU问题解析
    [其他]Android SDK离线文件路径以及安装更新方法
    [APP]如果你想反编译
    [Jmeter]jmeter之参数化
    常用性能相关工具
    DebugDiag收集Dump的使用说明
    Live Writer安装报错的问题,OnCatalogResult:0x80190194
    IIS不定期Crash和Oracle“未处理的内部错误(-2)”的问题分析
  • 原文地址:https://www.cnblogs.com/lifan12589/p/12145639.html
Copyright © 2011-2022 走看看