• xml转json高效率的java实现


    转载---  https://blog.csdn.net/tkggetg/article/details/47784321   

    <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <!--FASTJSON是当今处理json效率最高的json处理工具--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency>

    java代码实现

    
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    import org.dom4j.*;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    import java.util.List;
    
    /**
     * Created by chengsheng on 2015/8/19.
     */
    public class TestXml2Json {
        public static void main(String[] args) throws Exception {
            String xmlStr= readFile("D:/ADA/et/Issue_20130506_back.xml");
            Document doc= DocumentHelper.parseText(xmlStr);
            JSONObject json=new JSONObject();
            dom4j2Json(doc.getRootElement(),json);
            System.out.println("xml2Json:"+json.toJSONString());
    
        }
    
        public static String readFile(String path) throws Exception {
            File file=new File(path);
            FileInputStream fis = new FileInputStream(file);
            FileChannel fc = fis.getChannel();
            ByteBuffer bb = ByteBuffer.allocate(new Long(file.length()).intValue());
            //fc向buffer中读入数据
            fc.read(bb);
            bb.flip();
            String str=new String(bb.array(),"UTF8");
            fc.close();
            fis.close();
            return str;
    
        }
        /**
         * xml转json
         * @param xmlStr
         * @return
         * @throws DocumentException
         */
        public static JSONObject xml2Json(String xmlStr) throws DocumentException{
            Document doc= DocumentHelper.parseText(xmlStr);
            JSONObject json=new JSONObject();
            dom4j2Json(doc.getRootElement(), json);
            return json;
        }
    
        /**
         * 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;
        }
    }
  • 相关阅读:
    MIT 6.828 JOS学习笔记10. Lab 1 Part 3: The kernel
    Java基础知识点4:继承
    CentOS Installation
    超微主板创建RAID磁盘阵列
    MySQL查询语句
    Psql操作命令
    Let's Encrypt 免费 SSL 证书续期
    Linux系统禁用swap分区
    公共 NTP 服务器地址
    Postgresql配置
  • 原文地址:https://www.cnblogs.com/hmpcly/p/9674802.html
Copyright © 2020-2023  润新知