gpt4 book ai didi

xml - go - 标签中的属性/隐藏父标签 : SOAP envelope

转载 作者:数据小太阳 更新时间:2023-10-29 03:21:36 27 4
gpt4 key购买 nike

我正在尝试将 Go 结构编码为 Soap-Envelope (xml)。到目前为止,除了一个小错误外,皂体看起来还不错。虽然我的信封应该是这样的:

<SOAP-ENV:Body>
<q3:WMLS_AddToStore xmlns:q3="http://www.foo.abr/message/120">
<WMLtypeIn>param1</WMLtypeIn>
<XMLin>param2</XMLin>
<OptionsIn>param3</OptionsIn>
<CapabilitiesIn>param4</CapabilitiesIn>
</q3:WMLS_AddToStore>
</SOAP-ENV:Body>

我的代码正在生成这个:

<SOAP-ENV:Body>
<q1:WMLS_AddToStore xmlns:q1="http://www.foo.abr/message/120">
<ActionName>
<WMLtypeIn>param1</WMLtypeIn>
<XMLin>param2</XMLin>
<OptionsIn>param3</OptionsIn>
<CapabilitiesIn>param4</CapabilitiesIn>
</ActionName>
</q1:WMLS_AddToStore>
</SOAP-ENV:Body>

注意 ActionName 标签。我想:

删除这个 ActionName 标签

将其重命名为 q3:WMLS_AddToStore(我可以这样做),但随后我需要向其添加 xmlns:q1 属性。

代码:

type Message interface{}
type OperationWMLS_AddToStoreSoapIn struct {
WMLtypeIn string `xml:"WMLtypeIn,omitempty"`
XMLin string `xml:"XMLin,omitempty"`
OptionsIn string `xml:"OptionsIn,omitempty"`
CapabilitiesIn string `xml:"CapabilitiesIn,omitempty"`
}

type Body struct {
XMLName xml.Name `xml:"SOAP-ENV:Body"`
ActionName temperature `xml:"q1:WMLS_AddToStore"`
}

type Action struct {
ActionName Message `xml:",innerxml"`
XMLAttr string `xml:"xmlns:q1,attr"`
}

func main() {
in := struct {
OperationWMLS_AddToStoreSoapIn `xml:"tns:WMLS_AddToStore"`
}{
OperationWMLS_AddToStoreSoapIn{
"WMLtypeIn",
"XMLin",
"OptionsIn",
"CapabilitiesIn",
},
}

x := &Body{
ActionName: Action{
ActionName: in,
XMLAttr: "http://www.foo.abr/message/120",
},
}

enc := xml.NewEncoder(os.Stdout)
enc.Indent("", " ")
if err := enc.Encode(x); err != nil {
fmt.Printf("error: %v\n", err)
}

}

这是 Playground : https://play.golang.org/p/fWl-G2d8ME2

最佳答案

您如何看待这样重写您的代码 (https://play.golang.org/p/Kf5VpQeuThx):

package main

import (
"encoding/xml"
"fmt"
"os"
)

type s2 struct {
XMLName xml.Name `xml:"q3:WMLS_AddToStore"`
XMLAttr string `xml:"xmlns:q3,attr"`
WMLtypeIn string `xml:"WMLtypeIn,omitempty" json:"WMLtypeIn,omitempty" yaml:"WMLtypeIn,omitempty"`
XMLin string `xml:"XMLin,omitempty" json:"XMLin,omitempty" yaml:"XMLin,omitempty"`
OptionsIn string `xml:"OptionsIn,omitempty" json:"OptionsIn,omitempty" yaml:"OptionsIn,omitempty"`
CapabilitiesIn string `xml:"CapabilitiesIn,omitempty" json:"CapabilitiesIn,omitempty" yaml:"CapabilitiesIn,omitempty"`
}

type s1 struct {
XMLName xml.Name `xml:"SOAP-ENV:Body"`
S2 s2
}

func main() {
x := &s1{
S2: s2{
XMLAttr: "http://www.foo.abr/message/120",
WMLtypeIn: "WMLtypeIn",
XMLin: "XMLin",
OptionsIn: "OptionsIn",
CapabilitiesIn: "CapabilitiesIn",
},
}

enc := xml.NewEncoder(os.Stdout)
enc.Indent("", " ")
if err := enc.Encode(x); err != nil {
fmt.Printf("error: %v\n", err)
}
}

关于xml - go - 标签中的属性/隐藏父标签 : SOAP envelope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52878603/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com