duanli0162 2018-10-18 16:28
浏览 76
已采纳

go-标签/隐藏父标签中的属性:SOAP信封

I'm trying to encode Go struct into a Soap-Envelope (xml). So far, the soap body looks fine except one small error. While my envelope should look like this:

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

My code is Generating this:

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

Notice the ActionName tag. I want to either:

Remove this ActionName tag

OR

Rename it to q3:WMLS_AddToStore(which I can do) but then I need to add the xmlns:q1 attribute to it.

CODE:

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
", err)
}

}

Here's the playground: https://play.golang.org/p/fWl-G2d8ME2

  • 写回答

1条回答 默认 最新

  • douwen8118 2018-10-21 19:47
    关注

    What do you think of rewriting your code like that (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
    ", err)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化