Variable xml tag in golang

huangapple go评论95阅读模式
英文:

Variable xml tag in golang

问题

我正在尝试从我的Go程序与一个SOAP服务进行通信,但是我在使用xml包时遇到了困难。

我需要发送的大部分请求都具有以下格式:

<s:Envelope xmlns="namespace1">
 <s:Body>
      <FunctionName xmlns="namespace2"/>
 </s:Body>
</s:Envelope>

我感觉我需要为每个请求创建一个类型,因为FunctionName会变化... 这里是我目前使用的代码。

如果我能够有一个单一的类型,其中FunctionName作为一个属性,那就太好了,但是我就是无法弄清楚如何做到... 为了更清楚,我想在xml:"s:Body>FunctionName"中放一个变量代替FunctionName

非常感谢你的帮助!

英文:

I'm trying to communicate with a SOAP service from my go program but I'm having difficulties to use the xml package.

Most of the requests I have to send have the following format:

&lt;s:Envelope xmlns=&quot;namespace1&quot;&gt;
 &lt;s:Body&gt;
      &lt;FunctionName xmlns=“namespace2”/&gt;
 &lt;/s:Body&gt;
&lt;/s:Envelope&gt;

I feel I have to create one type for each request I want to make, as FunctionNamechanges... Here's the code I use so far.

It would be nice if I could have a single type with the FunctionName as an attribute but I just can't figure out how... To make it clearer, I would like to put a variable instead of FunctionName inside xml:&quot;s:Body&gt;FunctionName&quot;.

Thanks a lot for your help!

答案1

得分: 2

你可以使用xml.Name字段来指定你希望在XML输出中的标签名称。请注意,使用xml.Name,你还可以指定命名空间,因此你甚至不再需要Command.Field,你只需要用它来设置命名空间属性。

所以这是你修改后的代码:

type Command struct {
	XMLName xml.Name
}

type XMLEnvelop struct {
	XMLName      xml.Name `xml:"s:Envelope"`
	Xmlns        string   `xml:"xmlns:s,attr"`
	FunctionName Command  `xml:"s:Body>FunctionName"`
}

v := &XMLEnvelop{Xmlns: "namespace1",
    FunctionName: Command{xml.Name{"namespace2", "MyFuncName"}}}

output, err := xml.MarshalIndent(v, "", "    ")
if err != nil {
	fmt.Printf("error: %v\n", err)
}

// Write the output to check
os.Stdout.Write(output)

输出结果(在Go Playground上尝试):

<s:Envelope xmlns:s="namespace1">
    <s:Body>
        <MyFuncName xmlns="namespace2"></MyFuncName>
    </s:Body>
</s:Envelope>
英文:

You can use an xml.Name field to specify the tag name you want it in the XML output. Note that with xml.Name you can also specify the namespace, so you don't even need Command.Field anymore which you only used to set the namespace attribute.

So here is your modified code:

type Command struct {
	XMLName xml.Name
}

type XMLEnvelop struct {
	XMLName      xml.Name `xml:&quot;s:Envelope&quot;`
	Xmlns        string   `xml:&quot;xmlns:s,attr&quot;`
	FunctionName Command  `xml:&quot;s:Body&gt;FunctionName&quot;`
}

v := &amp;XMLEnvelop{Xmlns: &quot;namespace1&quot;,
    FunctionName: Command{xml.Name{&quot;namespace2&quot;, &quot;MyFuncName&quot;}}}

output, err := xml.MarshalIndent(v, &quot;&quot;, &quot;    &quot;)
if err != nil {
	fmt.Printf(&quot;error: %v\n&quot;, err)
}

// Write the output to check
os.Stdout.Write(output)

Output (try it on the Go Playground):

&lt;s:Envelope xmlns:s=&quot;namespace1&quot;&gt;
    &lt;s:Body&gt;
        &lt;MyFuncName xmlns=&quot;namespace2&quot;&gt;&lt;/MyFuncName&gt;
    &lt;/s:Body&gt;
&lt;/s:Envelope&gt;

huangapple
  • 本文由 发表于 2015年8月11日 15:16:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/31935453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定