Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

soap on node is parsing json to xml wrong

Using the Soap module in Nodejs (via loopback-connector-soap), I'm trying to convert json to xml for my soap request, but I have a few problems...

What it is:

<ns:UserDefinedFields>
    <ns:UserDefinedField>
        <ns:displayName>Campaign?</ns:displayName>
        <ns:fieldValue>Yes</ns:fieldValue>
    </ns:UserDefinedField>
</ns:UserDefinedFields>
<ns:UserDefinedFields>
    <ns:UserDefinedField>
        <ns:displayName>Anticipated Use</ns:displayName>
    <ns:fieldValue>6</ns:fieldValue>
</ns:UserDefinedField>

What it should be:

<ns:UserDefinedFields>
    <ns:UserDefinedField>
        <base:displayName>Campaign?</base:displayName>
        <base:fieldValue>Yes</base:fieldValue>
    </ns:UserDefinedField>
    <ns:UserDefinedField>
        <base:displayName>Anticipated Use</base:displayName>
        <base:fieldValue>6</base:fieldValue>
    </ns:UserDefinedField>
</ns:UserDefinedFields>
  • Should be only one <ns:UserDefinedFields>
  • And should be <base:fieldValue> instead of <ns:fieldValue>

Any advice would be super helpful! Thanks!

The XML:

{
    ...
    "UserDefinedFields": [
        {
            "displayName": "Campaign?",
            "fieldValue": "Yes"
        },
        {
            "displayName": "Anticipated Use",
            "fieldValue": 6
        }
    ]
    ...
}

The XSD:

...
<xs:element name="UserDefinedFields" minOccurs="0">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="UserDefinedField" type="base:UserDefinedFieldType" minOccurs="0" maxOccurs="15"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
...
like image 541
Geoffrey Burdett Avatar asked Jun 24 '15 18:06

Geoffrey Burdett


1 Answers

To deal with the namespace, pass the ignoredNamespaces the soap.createClient options.

{
    "ignoredNamespaces": {
        namespaces: ['ns']
    },
}

And then add the namespace manually. It's ugly, but it works.

To deal with multiple UserDefinedFields, it should look like this:

{
    . . .
    DomainRegistration['ns:UserDefinedFields']['ns:UserDefinedField'][0] : {
        "base:displayName": "Campaign?",
        "base:fieldValue": "Yes"
    },
    DomainRegistration['ns:UserDefinedFields']['ns:UserDefinedField'][1] : {
        "base:displayName": "Anticipated Use",
        "base:fieldValue": 6
    }
    . . .
}

Or to put it more simply:

{
    ouside: {
        insideA: [
            'one',
            'two',
        ]
        insideB: [
            {'one':'ONE'},
            {'two':'TWO'}
        ]
    }
}

//will create:

<outside>
    <insideA>
        <one />
        <two />
    </insideA>
    <insideB>
        <one>ONE</one>
    </insideB>
    <insideB>
        <two>TWO</two>
    </insideB>
</outside>
like image 134
Geoffrey Burdett Avatar answered Nov 02 '22 00:11

Geoffrey Burdett