Xml转JSON

import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class XmlToJson {
	/**
	 * 私有构造器.
	 */
	private XmlToJson() {
		
	}
	 
	 /**
	  * xmlToJson.
	  * @param xml xml
	  * @return JSONObject
	  * @throws Exception Exception
	  */
    public static JSONObject xmlToJson(final String xml) throws Exception {
        final JSONObject jsonObject = new JSONObject();
        final Document document = DocumentHelper.parseText(xml);
        //获取根节点元素对象
        final Element root = document.getRootElement();
        iterateNodes(root, jsonObject);
        return jsonObject;
    }
 
    /**
     * 遍历元素.
     *
     * @param node 元素
     * @param json 将元素遍历完成之后放的JSON对象
     */
    private static void iterateNodes(final Element node, final JSONObject json) {
        //获取当前元素的名称
        final String nodeName = node.getName();
        //判断已遍历的JSON中是否已经有了该元素的名称
        if (json.containsKey(nodeName)) {
            //该元素在同级下有多个
            final Object object = json.get(nodeName);
            JSONArray array = null;
            if (object instanceof JSONArray) {
                array = (JSONArray) object;
            } else {
                array = new JSONArray();
                array.add(object);
            }
 
            //获取该元素下所有子元素
            final List<Element> listElement = node.elements();
            if (listElement.isEmpty()) {
                //该元素无子元素,获取元素的值
                final String nodeValue = node.getTextTrim();
                array.add(nodeValue);
                json.put(nodeName, array);
                return;
            }
            //有子元素
            final JSONObject newJson = new JSONObject();
            //遍历所有子元素
            for (Element e : listElement) {
                //递归
                iterateNodes(e, newJson);
            }
            //获取该元素下所有属性
            final List<Attribute> attributeList = node.attributes();
            for (Attribute attribute : attributeList) {
                newJson.put(attribute.getName(), attribute.getValue());
            }
            array.add(newJson);
            json.put(nodeName, array);
            return;
        }
        //该元素同级下第一次遍历
        //获取该元素下所有子元素
        final List<Element> listElement = node.elements();
        if (listElement.isEmpty()) {
            //该元素无子元素,获取元素的值
            final String nodeValue = node.getTextTrim();
            json.put(nodeName, nodeValue);
            return;
        }
        //有子节点,新建一个JSONObject来存储该节点下子节点的值
        final JSONObject object = new JSONObject();
        //遍历所有一级子节点
        for (Element e : listElement) {
            //递归
            iterateNodes(e, object);
        }
        json.put(nodeName, object);
        //获取该元素下所有属性
        final List<Attribute> attributeList = node.attributes();
        for (Attribute attribute : attributeList) {
            json.put(attribute.getName(), attribute.getValue());
        }
        return;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值