BeNoZo
BeNoZo

Reputation: 35

Golang marshal nested xml tags

I'm trying to marshal a Soap xml response but cannot get nested tags (<ParameterValueStruct>) going, if i call the struct twice the last one overwrites the first one.

Any help is mostly appreciated.

Here is the sample code https://play.golang.org/p/fwb4FI0hDG9

Here is a sample xml im trying to mimic:

<soapenv:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:cwmp="urn:dslforum-org:cwmp-1-0" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
    <cwmp:ID soapenv:mustUnderstand="1">null0</cwmp:ID>
</soapenv:Header>
<soapenv:Body>
    <cwmp:SetParameterValues>
        <ParameterList soap:arrayType="cwmp:ParameterValueStruct[4]">
            <ParameterValueStruct>
                <Name>InternetGatewayDevice.ManagementServer.PeriodicInformEnable</Name>
                <Value xsi:type="xsd:boolean">1</Value>
            </ParameterValueStruct>
            <ParameterValueStruct>
                <Name>InternetGatewayDevice.ManagementServer.ConnectionRequestUsername</Name>
                <Value xsi:type="xsd:string">00147F-SpeedTouch780-CP0611JTLNW</Value>
            </ParameterValueStruct>
            <ParameterValueStruct>
                <Name>InternetGatewayDevice.ManagementServer.ConnectionRequestPassword</Name>
                <Value xsi:type="xsd:string">98ff55fb377bf724c625f60dec448646</Value>
            </ParameterValueStruct>
            <ParameterValueStruct>
                <Name>InternetGatewayDevice.ManagementServer.PeriodicInformInterval</Name>
                <Value xsi:type="xsd:unsignedInt">60</Value>
            </ParameterValueStruct>
        </ParameterList>
        <ParameterKey>null</ParameterKey>
    </cwmp:SetParameterValues>
</soapenv:Body>

Upvotes: 2

Views: 1046

Answers (1)

user9455968
user9455968

Reputation:

Define ParameterValueStruct to be a slice:

type SPVParameterList struct {
    XMLName              xml.Name                  `xml:"ParameterList"`
    ArrayType            string                    `xml:"soap:arrayType,attr"` // must use cwmp:ParameterValueStruct[?] Number of params to SPV
    ParameterValueStruct []SPVParameterValueStruct `xml:"ParameterValueStruct"`
}

Then build up the SPVParameterValueStructs and append them to ParameterValueStruct:

pvs1 := &SPVParameterValueStruct{
    Name: "Someparameter1",
    Value: SPVValue{
        Type:  "xsd:string",
        Value: "SomeParamValue1",
    },
}

pvs2 := &SPVParameterValueStruct{
    Name: "Someparameter2",
    Value: SPVValue{
        Type:  "xsd:string",
        Value: "SomeParamValue2",
    },
}

soap.Body.SPV.ParameterList.ParameterValueStruct =
    append(soap.Body.SPV.ParameterList.ParameterValueStruct, *pvs1, *pvs2)

see https://play.golang.org/p/As6ymGNViZx

Upvotes: 4

Related Questions