英文:
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
<?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>
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 "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
}
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:"https://route53.amazonaws.com/doc/2012-12-12/ CreateHostedZoneRequest"`
}
应该生成:
<CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2012-12-12/">
英文:
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:"https://route53.amazonaws.com/doc/2012-12-12/ CreateHostedZoneRequest"`
}
Should produce:
<CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2012-12-12/">
答案2
得分: 6
你可以这样做,这可能不是最优雅的解决方案,但似乎可以工作。
<!-- 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/",
}
生成
<?xml version="1.0" encoding="UTF-8"?>
<CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2012-12-12/">
<Name>DNS域名</Name>
<CallerReference>唯一描述</CallerReference>
<HostedZoneConfig>
<Comment>可选评论</Comment>
</HostedZoneConfig>
</CreateHostedZoneRequest>
英文:
You can do this, which possibly isn't the most elegant solution, but seems to work
<!-- 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 domain name",
CallerReference: "unique description",
Comment: "optional comment",
Xmlns: "https://route53.amazonaws.com/doc/2012-12-12/",
}
Producing
<?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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论