soap on node is parsing json to xml wrong
Asked Answered
H

1

14

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>
...
Heartburn answered 24/6, 2015 at 18:54 Comment(3)
Probably an issue with the library. Try creating an issue on the github page and/or use a different xml2json library.Flanigan
It probably shouldn't be <base:displayName></ns:displayName>. What you call The XML looks like JSON.Gates
Maybe you just confuse prefixes with namespaces. If the prefix ns in your XML is pointing to the same namespace as base in your XSD, everything is fine. The question does not contain enough information to judge about this. Please add the xmlns:ns= and xmlns:base= parts.Gates
H
1

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>
Heartburn answered 7/7, 2015 at 13:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.