dryift6733 2018-03-21 05:08
浏览 57
已采纳

Golang元帅嵌套XML标记

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>

  • 写回答

1条回答 默认 最新

  • dsqdpn31467 2018-03-21 07:57
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题