xml字符串转json对象

本文介绍了一种将XML数据解析并转换为JSON格式的方法。该方法首先使用DOMParser或Microsoft XMLDOM解析XML字符串,然后递归地遍历XML节点以构建相应的JSON对象。此过程考虑了属性、文本节点及子节点的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

function parseXML(data) {
    var xml, tmp;
    if (window.DOMParser) {
        Standard tmp = new DOMParser();
        xml = tmp.parseFromString(data, "text/xml");
    } else {
        // IE 
        xml = new ActiveXObject("Microsoft.XMLDOM");
        xml.async = "false";
        xml.loadXML(data);
    }
    tmp = xml.documentElement;
    if (!tmp || !tmp.nodeName || tmp.nodeName === "parsererror") {
        return null;
    }
    return xml;
};


function toJson(obj) {
    if (this == null) return null;
    var retObj = new Object;
    buildObjectNode(retObj, /*jQuery*/ obj);
    return retObj;


    function buildObjectNode(cycleOBJ, /*Element*/ elNode) { /*NamedNodeMap*/
        var nodeAttr = elNode.attributes;
        if (nodeAttr != null) {
            if (nodeAttr.length && cycleOBJ == null) cycleOBJ = new Object;
            for (var i = 0; i < nodeAttr.length; i++) {
                cycleOBJ[nodeAttr[i].name] = nodeAttr[i].value;
            }
        }
        var nodeText = "text";
        if (elNode.text == null) nodeText = "textContent"; /*NodeList*/
        var nodeChilds = elNode.childNodes;
        if (nodeChilds != null) {
            if (nodeChilds.length && cycleOBJ == null) cycleOBJ = new Object;
            for (var i = 0; i < nodeChilds.length; i++) {
                if (nodeChilds[i].tagName != null) {
                    if (nodeChilds[i].childNodes[0] != null && nodeChilds[i].childNodes.length <= 1 && (nodeChilds[i].childNodes[0].nodeType == 3 || nodeChilds[i].childNodes[0].nodeType == 4)) {
                        if (cycleOBJ[nodeChilds[i].tagName] == null) {
                            cycleOBJ[nodeChilds[i].tagName] = nodeChilds[i][nodeText];
                        } else {
                            if (typeof(cycleOBJ[nodeChilds[i].tagName]) == "object" && cycleOBJ[nodeChilds[i].tagName].length) {
                                cycleOBJ[nodeChilds[i].tagName][cycleOBJ[nodeChilds[i].tagName].length] = nodeChilds[i][nodeText];
                            } else {
                                cycleOBJ[nodeChilds[i].tagName] = [cycleOBJ[nodeChilds[i].tagName]];
                                cycleOBJ[nodeChilds[i].tagName][1] = nodeChilds[i][nodeText];
                            }
                        }
                    } else {
                        if (nodeChilds[i].childNodes.length) {
                            if (cycleOBJ[nodeChilds[i].tagName] == null) {
                                cycleOBJ[nodeChilds[i].tagName] = new Object;
                                buildObjectNode(cycleOBJ[nodeChilds[i].tagName], nodeChilds[i]);
                            } else {
                                if (cycleOBJ[nodeChilds[i].tagName].length) {
                                    cycleOBJ[nodeChilds[i].tagName][cycleOBJ[nodeChilds[i].tagName].length] = new Object;
                                    buildObjectNode(cycleOBJ[nodeChilds[i].tagName][cycleOBJ[nodeChilds[i].tagName].length - 1], nodeChilds[i]);
                                } else {
                                    cycleOBJ[nodeChilds[i].tagName] = [cycleOBJ[nodeChilds[i].tagName]];
                                    cycleOBJ[nodeChilds[i].tagName][1] = new Object;
                                    buildObjectNode(cycleOBJ[nodeChilds[i].tagName][1], nodeChilds[i]);
                                }
                            }
                        } else {
                            cycleOBJ[nodeChilds[i].tagName] = nodeChilds[i][nodeText];
                        }
                    }
                }
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值