xml格式转换成json格式

一、xml格式转换成json格式

package com.herocheer.bms.egjjfw.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.*;

import java.util.List;

public class xmlAndJson {
     
    public static void main(String[] args) {
     
        try {
     
            String rest = "\n" +
                    "\n" +
                    "\n" +
                    "  \n" +
                    "    \n" +
                    "    11\n" +
                    "    \n" +
                    "    \n" +
                    "    \n" +
                    "    \n" +
                    "    000\n" +
                    "    交易成功\n" +
                    "    20200909\n" +
                    "    \n" +
                    "    6029\n" +
                    "    \n" +
                    "    1599615034960\n" +
                    "  \n" +
                    "  \n" +
                    "    500\n" +
                    "    19\n" +
                    "    \n" +
                    "      70\n" +
                    "      1310000000\n" +
                    "      阿里巴巴\n" +
                    "      C11112\n" +
                    "    \n" +
                    "    \n" +
                    "      71\n" +
                    "      13110000000\n" +
                    "      宇宙无敌天下商会\n" +
                    "      C11114\n" +
                    "    \n" +
                    "    \n" +
                    "      72\n" +
                    "      140900000\n" +
                    "      娃哈哈有限公司\n" +
                    "      C11111\n" +
                    "    \n" +
                    "  \n" +
                    "";

            JSONObject jsonObject = xml2Json(rest);
            System.out.println(jsonObject);
        } catch (DocumentException e) {
     
            e.printStackTrace();
        }

    }

    /**
     *  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;
    }
    public static void dom4j2Json(Element element, JSONObject json) {
     
        // 如果是属性
        for (Object o : element.attributes()) {
     
            Attribute attr = (Attribute) o;
            if (StringUtils.isNotEmpty(attr.getValue())) {
     
                json.put("@" + attr.getName(), attr.getValue());
            }
        }
        List<Element> chdEl = element.elements();
        if (chdEl.isEmpty() && StringUtils.isNotEmpty(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 (StringUtils.isNotEmpty(attr.getValue())) {
     
                        json.put("@" + attr.getName(), attr.getValue());
                    }
                }
                if (!e.getText().isEmpty()) {
     
                    json.put(e.getName(), e.getText());
                }
            }
        }
    }
}

控制台输出:

{
     "HEAD":{
     "Msg":"交易成功","ChanCode":"11","Time":"093035","TrsCode":"6029","DemoSerial":"1599615034960","Code":"000","Date":"20200909"},
"BODY":{
     "total":"500","pagesize":"19","rows":[{
     "xh":"70","zxjedhwe":"C11112","company":"阿里巴巴","demo":"1310000000"},
{
     "xh":"71","zxjedhwe":"C11114","company":"宇宙无敌天下商会","demo":"13110000000"},
{
     "xh":"72","zxjedhwe":"C11111","company":"娃哈哈有限公司","demo":"140900000"}]}}

你可能感兴趣的:(java,json,xml,dom)