SOAP对JSON的响应(XML)

SOAP response (XML) to JSON

本文关键字:XML 响应 JSON SOAP      更新时间:2023-09-26

我需要使用SOAP web服务,它自然会以XML发送响应,因为我正在开发Appcelerator Titanium移动应用程序,所以我更喜欢JSON格式的响应。在线查看后,我使用此Javascript代码转换了响应,它基本上起作用,但返回了以下结果:

{
    "SOAP-ENV:Body" :     {
        "ns1:linkAppResponse" :         {
            "ns1:result" :             {
                #text : true;
            };
            "ns1:uuid" :             {
                #text : "a3dd915e-b4e4-43e0-a0e7-3c270e5e7aae";
            };
        };
    };
}

当然,中的冒号和散列引起了问题,所以我调整了代码,在名称上添加了一个子字符串,并删除了":"之前的任何内容,然后对生成的JSON进行了字符串化,删除了所有散列,并再次解析了JSON。这对我来说有点乱,但我最终得到了一些有用的东西。

以下是我正在使用的xmlToJson代码:

// Changes XML to JSON
function xmlToJson(xml) {
    // Create the return object
    var obj = {};
    if (xml.nodeType == 1) {// element
        // do attributes
        if (xml.attributes.length > 0) {
            obj["@attributes"] = {};
            for (var j = 0; j < xml.attributes.length; j++) {
                var attribute = xml.attributes.item(j);
                obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
            }
        }
    } else if (xml.nodeType == 3) {// text
        obj = xml.nodeValue;
    }
    // do children
    if (xml.hasChildNodes()) {
        for (var i = 0; i < xml.childNodes.length; i++) {
            var item = xml.childNodes.item(i);
            var nodeName = item.nodeName.substring(item.nodeName.indexOf(":") + 1);
            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));
            }
        }
    }
    return obj;
};
module.exports = xmlToJson; 

这导致了以下JSON:

{
    Body :     {
        linkAppResponse :         {
            result :             {
                text : true;
            };
            uuid :             {
                text : "9022d249-ea8a-47a3-883c-0f4cfc9d6494";
            };
        };
    };
}

虽然这返回了一个我可以使用的JSON对象,但我更希望得到以下形式的JSON:

{
    result : true;
    uuid : "9022d249-ea8a-47a3-883c-0f4cfc9d6494";
};

大多数情况下,它不那么冗长,我可以简单地调用json.result来检查查询是否成功,而不是json。Body.linkAppResponse.result.text

非常感谢您的帮助。

提出了一个有效的解决方案,虽然不那么脏,但它可以工作,并以我想要的格式返回数据。

function soapResponseToJson(xml) {
    var json = xmlToJson(xml).Body;
    console.debug(json);
    var response = {};
    for (var outterKey in json) {
        if (json.hasOwnProperty(outterKey)) {
            temp = json[outterKey];
            for (var innerKey in temp) {
                if (temp.hasOwnProperty(innerKey)) {
                    response[innerKey] = temp[innerKey].text;
                }
            }
        }
    }
    console.debug(response);
    return response;
}
// Changes XML to JSON
function xmlToJson(xml) {
    // Create the return object
    var obj = {};
    if (xml.nodeType == 1) {// element
        // do attributes
        if (xml.attributes.length > 0) {
            obj["@attributes"] = {};
            for (var j = 0; j < xml.attributes.length; j++) {
                var attribute = xml.attributes.item(j);
                obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
            }
        }
    } else if (xml.nodeType == 3) {// text
        obj = xml.nodeValue;
    }
    // do children
    if (xml.hasChildNodes()) {
        for (var i = 0; i < xml.childNodes.length; i++) {
            var item = xml.childNodes.item(i);
            var nodeName = item.nodeName.substring(item.nodeName.indexOf(":") + 1).replace('#', '');
            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));
            }
        }
    }
    return obj;
};
module.exports = soapResponseToJson;

console.debug(json):

{
    linkAppResponse :     {
        result :         {
            text : true;
        };
        uuid :         {
            text : "e4f78c5f-1bc2-4b50-a749-19d733b9be3f";
        };
    };
}

console.debug(响应):

{
    result : true;
    uuid : "e4f78c5f-1bc2-4b50-a749-19d733b9be3f";
}

我将暂时不提这个问题,以防有人想出更好的解决方案。

我觉得这是一个相当丑陋的解决方案(希望它不会冒犯您:)。为什么不将xml封送到一个对象,然后使用gson或jackson映射到json呢。我不知道您使用的是什么框架,例如在春季,您可以使用jaxb2进行封送,使用jackson或gson将对象转换为json。