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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀