栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

将XML转成JSON方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将XML转成JSON方法

学习过程中,有需要将XML转成JSON,封装了个类,记录一下

    
    public static JSONObject xml2Json(String xmlStr) throws DocumentException, UnsupportedEncodingException {
        //检查传过来的字符编码,编码
        //字符编码出错,会报错(Java Error on line 1 of document : 前言中不允许有内容.)
        //重新编码解决问题
        String IsCode =getEncoding(xmlStr);
        xmlStr = xmlStr.replace("n","").replace("t","").trim();
        String xmlstr2 = new String(xmlStr.getBytes("UTF-8"),"UTF-8");
        System.out.println(xmlstr2);
        Document doc = DocumentHelper.parseText(xmlstr2);
        JSONObject json = new JSONObject();
        dom4j2Json(doc.getRootElement(), json);
        return json;
    }

    

    public static String getEncoding(String str) {
        String encode = "GB2312";
        try {
            //判断是不是GB2312
            if (isEncoding(str, encode)) {
                return encode;
            }
        } catch (Exception exception) {
            return "编码错误";
        }
        encode = "ISO-8859-1";
        try {
            //判断是不是ISO-8859-1
            if (isEncoding(str, encode)) {
                return encode;
            }
        } catch (Exception exception1) {
            return "编码错误";
        }
        encode = "UTF-8";
        try {
            //判断是不是UTF-8
            if (isEncoding(str, encode)) {
                return encode;
            }
        } catch (Exception exception2) {
            return "编码错误";
        }
        encode = "GBK";
        try {
            //判断是不是GBK
            if (isEncoding(str, encode)) {
                return encode;
            }
        } catch (Exception exception3) {
            return "编码错误";
        }
        //如果都不是,说明输入的内容不属于常见的编码格式。
        return "不属于常见的编码格式";
    }
    
    public static boolean isEncoding(String str, String encode) {
        try {
            if (str.equals(new String(str.getBytes(), encode))) {
                return true;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return false;
    }


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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995715.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号