zoukankan      html  css  js  c++  java
  • xml转json

    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.input.SAXBuilder;
     
     
        public static  String xmlToJSON(String xml,int jsonType) {   
            JSONObject obj = new JSONObject();   
            try {   
                InputStream is = new ByteArrayInputStream(xml.getBytes("utf-8"));   
                SAXBuilder sb = new SAXBuilder();   
                Document doc = sb.build(is);   
                Element root = doc.getRootElement();   
                Map map=iterateElement(root);   
               obj.put(root.getName(),map);   
               return obj.toString();   
            } catch (Exception e) {   
                e.printStackTrace();   
                return null;   
            }   
        }   
       
       
        private static Map  iterateElement(Element root) {   
            List childrenList = root.getChildren();   
            Element element = null;   
            Map map = new HashMap();   
            List list = null;   
            for (int i = 0; i < childrenList.size(); i++) {   
                list = new ArrayList();   
                element = (Element) childrenList.get(i);   
                if(element.getChildren().size()>0){   
                    if(root.getChildren(element.getName()).size()>1){   
                        if (map.containsKey(element.getName())) {   
                            list = (List) map.get(element.getName());   
                        }   
                        list.add(iterateElement(element));   
                        map.put(element.getName(), list);   
                    }else{   
                        map.put(element.getName(), iterateElement(element));   
                    }   
                }else {   
                    if(root.getChildren(element.getName()).size()>1){   
                        if (map.containsKey(element.getName())) {   
                            list = (List) map.get(element.getName());   
                        }   
                        list.add(element.getTextTrim());   
                        map.put(element.getName(), list);   
                    }else{   
                        map.put(element.getName(), element.getTextTrim());   
                    }   
                }   
            }   
               
            return map;   
        }   
  • 相关阅读:
    Insert Buffering
    B-Tree vs LSM-tree
    MySQL 5.6 死锁演示 及 日志分析
    MySQL索引
    InnoDB的三个关键特性
    MySQL如何优化GROUP BY :松散索引扫描 VS 紧凑索引扫描
    MySql 自适应哈希索引
    母牛的故事
    简单的java程序
    一些变量的比较
  • 原文地址:https://www.cnblogs.com/bchange/p/9181998.html
Copyright © 2011-2022 走看看