Marshalling XML Go XMLName + xmlns

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

Marshalling XML Go XMLName + xmlns

问题

我正在尝试实现以下XML输出

<?xml version="1.0" encoding="UTF-8"?>

<CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2012-12-12/">
   <Name>DNS domain name</Name>
   <CallerReference>unique description</CallerReference>
   <HostedZoneConfig>
      <Comment>optional comment</Comment>
   </HostedZoneConfig>
</CreateHostedZoneRequest>

我有以下代码,输出的XML非常接近,但是我无法将xmlns编码到CreateHostedZoneRequest中

xmlns="https://route53.amazonaws.com/doc/2012-12-12/

package main

import "fmt"
import "encoding/xml"

type ZoneRequest struct {
  Name            string
  CallerReference string
  Comment         string `xml:"HostedZoneConfig>Comment"`
}

var zoneRequest = ZoneRequest{
  Name:            "DNS domain name",
  CallerReference: "unique description",
  Comment:         "optional comment",
}

func main() {
  tmp, _ := createHostedZoneXML(zoneRequest)
  fmt.Println(tmp)
}

func createHostedZoneXML(zoneRequest ZoneRequest) (response string, err error) {
  tmp := struct {
    ZoneRequest
    XMLName struct{} `xml:"CreateHostedZoneRequest"`
  }{ZoneRequest: zoneRequest}

  byteXML, err := xml.MarshalIndent(tmp, "", "   ")
  if err != nil {
    return "", err
  }
  response = xml.Header + string(byteXML)
  return
}

如何将xmlns编码到CreateHostedZoneRequest中?

英文:

I am trying to achive the following XML output

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;CreateHostedZoneRequest xmlns=&quot;https://route53.amazonaws.com/doc/2012-12-12/&quot;&gt;
   &lt;Name&gt;DNS domain name&lt;/Name&gt;
   &lt;CallerReference&gt;unique description&lt;/CallerReference&gt;
   &lt;HostedZoneConfig&gt;
      &lt;Comment&gt;optional comment&lt;/Comment&gt;
   &lt;/HostedZoneConfig&gt;
&lt;/CreateHostedZoneRequest&gt;

I have the following which outputs XML that is very close however I have been unable to encode into CreateHostedZoneRequest
>xmlns="https://route53.amazonaws.com/doc/2012-12-12/

<!-- language: lang-golang -->

package main

import &quot;fmt&quot;
import &quot;encoding/xml&quot;

type ZoneRequest struct {
  Name            string
  CallerReference string
  Comment         string `xml:&quot;HostedZoneConfig&gt;Comment&quot;`
}

var zoneRequest = ZoneRequest{
  Name:            &quot;DNS domain name&quot;,
  CallerReference: &quot;unique description&quot;,
  Comment:         &quot;optional comment&quot;,
}

func main() {
  tmp, _ := createHostedZoneXML(zoneRequest)
  fmt.Println(tmp)
}

func createHostedZoneXML(zoneRequest ZoneRequest) (response string, err error) {
  tmp := struct {
    ZoneRequest
    XMLName struct{} `xml:&quot;CreateHostedZoneRequest&quot;`
  }{ZoneRequest: zoneRequest}

  byteXML, err := xml.MarshalIndent(tmp, &quot;&quot;, `   `)
  if err != nil {
    return &quot;&quot;, err
  }
  response = xml.Header + string(byteXML)
  return
}

http://play.golang.org/p/pyK76VPD5-

How can I encode xmlns into the CreateHostedZoneRequest?

答案1

得分: 18

我有一个类似的问题。Unmarshal方法的文档(http://golang.org/pkg/encoding/xml/#Unmarshal)中有以下内容:

> 如果XMLName字段有一个形如“name”或“namespace-URL name”的相关标签,那么XML元素必须具有给定的名称(和可选的命名空间),否则Unmarshal将返回一个错误。

在结构体标签中使用“namespace-URL name”:

<!-- language: lang-go -->

type ZoneRequest struct {
	XMLName  xml.Name `xml:&quot;https://route53.amazonaws.com/doc/2012-12-12/ CreateHostedZoneRequest&quot;`
}

应该生成:

&lt;CreateHostedZoneRequest xmlns=&quot;https://route53.amazonaws.com/doc/2012-12-12/&quot;&gt;
英文:

I had a similar question. The docs for the Unmarshal method (http://golang.org/pkg/encoding/xml/#Unmarshal) have:

> If the XMLName field has an associated tag of the form "name" or "namespace-URL name", the XML element must have the given name (and, optionally, name space) or else Unmarshal returns an error.

Using "namespace-URL name" in the struct tag:

<!-- language: lang-go -->

type ZoneRequest struct {
	XMLName  xml.Name `xml:&quot;https://route53.amazonaws.com/doc/2012-12-12/ CreateHostedZoneRequest&quot;`
}

Should produce:

&lt;CreateHostedZoneRequest xmlns=&quot;https://route53.amazonaws.com/doc/2012-12-12/&quot;&gt;

答案2

得分: 6

你可以这样做,这可能不是最优雅的解决方案,但似乎可以工作。

Playground链接

<!-- language: lang-golang -->

type ZoneRequest struct {
    Name            string
    CallerReference string
    Comment         string `xml:"HostedZoneConfig>Comment"`
    Xmlns           string `xml:"xmlns,attr"`
}

var zoneRequest = ZoneRequest{
    Name:            "DNS域名",
    CallerReference: "唯一描述",
    Comment:         "可选评论",
    Xmlns:           "https://route53.amazonaws.com/doc/2012-12-12/",
}

生成

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2012-12-12/"&gt;
   &lt;Name&gt;DNS域名&lt;/Name&gt;
   &lt;CallerReference&gt;唯一描述&lt;/CallerReference&gt;
   &lt;HostedZoneConfig&gt;
      &lt;Comment&gt;可选评论&lt;/Comment&gt;
   &lt;/HostedZoneConfig&gt;
&lt;/CreateHostedZoneRequest&gt;
英文:

You can do this, which possibly isn't the most elegant solution, but seems to work

Playground link

<!-- language: lang-golang -->

type ZoneRequest struct {
	Name            string
	CallerReference string
	Comment         string `xml:&quot;HostedZoneConfig&gt;Comment&quot;`
	Xmlns           string `xml:&quot;xmlns,attr&quot;`
}

var zoneRequest = ZoneRequest{
	Name:            &quot;DNS domain name&quot;,
	CallerReference: &quot;unique description&quot;,
	Comment:         &quot;optional comment&quot;,
	Xmlns:           &quot;https://route53.amazonaws.com/doc/2012-12-12/&quot;,
}

Producing

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;CreateHostedZoneRequest xmlns=&quot;https://route53.amazonaws.com/doc/2012-12-12/&quot;&gt;
   &lt;Name&gt;DNS domain name&lt;/Name&gt;
   &lt;CallerReference&gt;unique description&lt;/CallerReference&gt;
   &lt;HostedZoneConfig&gt;
      &lt;Comment&gt;optional comment&lt;/Comment&gt;
   &lt;/HostedZoneConfig&gt;
&lt;/CreateHostedZoneRequest&gt;

huangapple
  • 本文由 发表于 2013年4月25日 05:05:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/16202170.html
匿名

发表评论

匿名网友

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

确定