如何从JSON创建SOAP Request BODY?

时间:2019-06-18 14:29:16

标签: node.js json xml libxml-js xml-to-json

我创建了一个node.js应用程序,用于从JSON对象创建SOAP Request BODY。我有以下代码

var convert = require('xml-js');
var xml = `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:tem="http://tempuri.org/" xmlns:tel="http://schemas.datacontract.org" 
xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<soapenv:Header/>
<soapenv:Body>
<tem:MyRequest>
 <tem:request>
<tel:MyTag1>null</tel:MyTag1>
<tel:MyTag2>12</tel:MyTag2>
<tel:MyTag3>MyValue1</tel:MyTag3>
<tel:MyTag4><arr:int>1000</arr:int>
<arr:int>1001</arr:int><arr:int>1002</arr:int>
</tel:MyTag4>
</tem:request>
</tem:MyRequest>
</soapenv:Body>
</soapenv:Envelope>`;

var options = { ignoreComment: false, alwaysChildren: true };
var result1 = convert.xml2js(xml, options);
console.log("json = ",JSON.stringify(result1));


var options2 = {compact: true, ignoreComment: true, spaces: 4};
var result2 = convert.json2xml(result1, options2);
console.log("xml ==> ",result2);

我得到了这样的输出

json =  {"elements":[{"type":"element","name":"soapenv:Envelope","attributes":{"xmlns:soapenv":"http://schemas.xmlsoap.org/soap/envelope/","xmlns:tem":"http://tempuri.org/","xmlns:tel":"http://schemas.datacontract.org","xmlns:arr":"http://schemas.microsoft.com/2003/10/Serialization/Arrays"},"elements":[{"type":"element","name":"soapenv:Header","elements":[]},{"type":"element","name":"soapenv:Body","elements":[{"type":"element","name":"tem:MyRequest","elements":[{"type":"element","name":"tem:request","elements":[{"type":"element","name":"tel:MyTag1","elements":[{"type":"text","text":"null"}]},{"type":"element","name":"tel:MyTag2","elements":[{"type":"text","text":"12"}]},{"type":"element","name":"tel:MyTag3","elements":[{"type":"text","text":"MyValue1"}]},{"type":"element","name":"tel:MyTag4","elements":[{"type":"element","name":"arr:int","elements":[{"type":"text","text":"1000"}]},{"type":"element","name":"arr:int","elements":[{"type":"text","text":"1001"}]},{"type":"element","name":"arr:int","elements":[{"type":"text","text":"1002"}]}]}]}]}]}]}]}


xml ==>  <elements>
    <type>element</type>
    <name>soapenv:Envelope</name>
    <attributes>
        <xmlns:soapenv>http://schemas.xmlsoap.org/soap/envelope/</xmlns:soapenv>
        <xmlns:tem>http://tempuri.org/</xmlns:tem>
        <xmlns:tel>http://schemas.datacontract.org</xmlns:tel>
     <xmlns:arr>http://schemas.microsoft.com/2003/10/Serialization/Arrays</xmlns:arr>
    </attributes>
    <elements>
        <type>element</type>
        <name>soapenv:Header</name>
    </elements>
    <elements>
        <type>element</type>
        <name>soapenv:Body</name>
        <elements>
            <type>element</type>
            <name>tem:MyRequest</name>
            <elements>
                <type>element</type>
                <name>tem:request</name>
                <elements>
                    <type>element</type>
                    <name>tel:MyTag1</name>
                    <elements>
                        <type>text</type>
                        <text>null</text>
                    </elements>
                </elements>
                <elements>
                    <type>element</type>
                    <name>tel:MyTag2</name>
                    <elements>
                        <type>text</type>
                        <text>12</text>
                    </elements>
                </elements>
                <elements>
                    <type>element</type>
                    <name>tel:MyTag3</name>
                    <elements>
                        <type>text</type>
                        <text>MyValue1</text>
                    </elements>
                </elements>
                <elements>
                    <type>element</type>
                    <name>tel:MyTag4</name>
                    <elements>
                        <type>element</type>
                        <name>arr:int</name>
                        <elements>
                            <type>text</type>
                            <text>1000</text>
                        </elements>
                    </elements>
                    <elements>
                        <type>element</type>
                        <name>arr:int</name>
                        <elements>
                            <type>text</type>
                            <text>1001</text>
                        </elements>
                    </elements>
                    <elements>
                        <type>element</type>
                        <name>arr:int</name>
                        <elements>
                            <type>text</type>
                            <text>1002</text>
                        </elements>
                    </elements>
                </elements>
            </elements>
        </elements>
    </elements>
</elements>

输入xml和最终xml不匹配。为什么这个?

0 个答案:

没有答案