Golang marshal嵌套的xml标签

时间:2018-03-21 05:08:42

标签: xml go soap marshalling

我试图编组Soap xml响应,但无法获取嵌套标记(<ParameterValueStruct>),如果我调用结构两次,最后一个覆盖第一个。

任何帮助都非常感谢。

以下是示例代码https://play.golang.org/p/fwb4FI0hDG9

这是一个尝试模仿的xml示例:

<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>

1 个答案:

答案 0 :(得分:3)

ParameterValueStruct定义为切片:

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"`
}

然后建立SPVParameterValueStructappend他们到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)

请参阅https://play.golang.org/p/As6ymGNViZx