• 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;
        }
        
        
        
        
        
    }
  • 相关阅读:
    趁热讲讲skin.xml支持的标签和attributes
    如何配置和编译ogre 1.7.0 + cegui 0.7.1
    关于OGRE基础教程6中CEGUI的layout文件can not locate的问题
    skin.xml皮肤配置讲解
    OCX控件注册相关(检查是否注册,注册,反注册)
    重回博客园继续我的 GUI库
    窗口类的定义
    UI库需要完成的任务
    屏幕截图代码
    深入C++的默认构造函数1
  • 原文地址:https://www.cnblogs.com/lifan12589/p/12145639.html
Copyright © 2020-2023  润新知