在 Javascript 中将 xml 转换为 json

convert xml to json in Javascript

本文关键字:json 转换 xml Javascript 中将      更新时间:2023-09-26

我有以下javascript函数(我从Stack Overflow获得的),它将XML转换为JSON:

function xmlToJson(xml) {
    try {
        var obj = {};
        if (xml.nodeType == 1) {
            if (xml.attributes.length > 0) {
                for (var j = 0; j < xml.attributes.length; j++) {
                    var attribute = xml.attributes.item(j);
                    obj[attribute.nodeName] = attribute.nodeValue;
                }
            }
        } else if (xml.nodeType == 3) {
            obj = xml.nodeValue;
        }
        if (xml.hasChildNodes()) {
            for (var i = 0; i < xml.childNodes.length; i++) {
                var item = xml.childNodes.item(i);
                var nodeName = item.nodeName;
                if (typeof (obj[nodeName]) == "undefined") {
                    obj[nodeName] = xmlToJson(item);
                } else {
                    if (typeof (obj[nodeName].push) == "undefined") {
                        var old = obj[nodeName];
                        obj[nodeName] = [];
                        obj[nodeName].push(old);
                    }
                    obj[nodeName].push(xmlToJson(item));
                }
            }
        }
        console.log(JSON.stringify(obj));
        return obj;
    } catch (e) {
        alert(e.message);
    }
}

我想要的是当 xml 节点至少具有单个子节点并且它还具有父节点时将其作为数组 ([]) 返回。在此代码中,如果 xml 节点具有单个子节点,则返回 map ({}),但对于多个子节点,则返回映射 ({})。

例如,我想要 XML

<pnode attr1="abc">
    <cnode attr2="xyz"></cnode>
</pnode>

要转换为 JSON

{
    "pnode": {
        "attr1": "abc"
    },
    "cnode": [
        {"attr2": "xyz"}
    ]
}

澄清了您想要实现的目标,这里有一个算法。我会留下我的另一个答案,因为我仍然认为最明智的选择是不玩结构

function flattenNodes(node, isChild) {
    var obj = {}, obj2, i, key, attributes = {};
    if (node.attributes && node.attributes.length)
        for (i = 0; i < node.attributes.length; ++i)
            attributes[node.attributes[i].nodeName] = node.attributes[i].nodeValue;
    if (!isChild)
        obj[node.nodeName] = attributes;
    else {
        if (!obj.hasOwnProperty(node.nodeName))
            obj[node.nodeName] = [];
        else if (!(obj[node.nodeName] instanceof Array))
            obj[node.nodeName] = [obj[node.nodeName]];
        obj[node.nodeName].push(attributes);
    }
    attributes = null; // free
    if (node.childNodes && node.childNodes.length)
        for (i = 0; i < node.childNodes.length; ++i) {
            if (node.childNodes[i].nodeType === 3) continue; // skip text node
            obj2 = flattenNodes(node.childNodes[i], 1); // recurse
            for (key in obj2) // merge
                if (obj2.hasOwnProperty(key))
                    if (!obj.hasOwnProperty(key)) {
                        obj[key] = obj2[key];
                    } else {
                        if (!(obj[key] instanceof Array))
                            obj[key] = [obj[key]];
                        obj[key] = obj[key].concat(obj2[key]);
                    }
        }
    return obj;
}

节点 root_node 上的用法示例

var root_node;
root_node = new DOMParser().parseFromString(
    '<pnode attr1="abc"><cnode attr2="xyz"></cnode></pnode>',
    'text/xml'
).documentElement;
var o = flattenNodes(root_node); // create
JSON.stringify(o);               // to JSON
// {"pnode":{"attr1":"abc"},"cnode":[{"attr2":"xyz"}]}

如果你有<foo bar="baz"><foo hello="world"></foo></foo>形式的XML,第一次迭代会导致{foo: {bar: "baz"}},然后第二次遇到会把它修改为数组形式{foo: [{bar: "baz"}, {hello: "world"}]}

我会以不同的方式形成表示 XML 的对象;

Integer  nodeType
String   nodeName
String   nodeValue
Array    childNodes
Object   attributes

现在,您可以拥有与子节点数量/等无关的相同形式

function nodeToObject(node) {
    var obj = {}, i;
    obj.nodeType = node.nodeType;
    obj.nodeName = node.nodeName;
    obj.nodeValue = node.nodeValue;
    obj.childNodes = [];
    obj.attributes = {};
    if (node.childNodes && node.childNodes.length)
        for (i = 0; i < node.childNodes.length; ++i)
            obj.childNodes.push(nodeToObject(node.childNodes[i]));
    if (node.attributes && node.attributes.length)
        for (i = 0; i < node.attributes.length; ++i)
            obj.attributes[node.attributes[i].nodeName] = node.attributes[i].nodeValue;
    return obj;
}

然后将root_node转换为 JSON,

JSON.stringify(nodeToObject(root_node));

JavaScript 中也可以朝相反的方向发展,使用一些基于 nodeType 的小逻辑来选择创建方法。