xml - Go XML 编码的问题

标签 xml go encoding

我需要生成以下 xml:

<AccessControlList>
    <Grant>
        <Grantee
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
            <ID>xxxx-xx</ID>
            <DisplayName>rahul.khan</DisplayName>
        </Grantee>
        <Permission>FULL_CONTROL</Permission>
    </Grant>
</AccessControlList>

我的结构是这样定义的:

type Grantee struct {
Xmlns_xsi   string `xml:"xmlns xsi,attr,omitempty"`
Xsi_type    string `xml:"http://www.w3.org/2001/XMLSchema-instance type,attr,omitempty"`
ID          string `xml:",omitempty"`
DisplayName string `xml:",omitempty"`

然而,当我编码此结构时,生成的结果 xml 文档如下所示:

<AccessControlList>
    <Grant>
        <Grantee
            xmlns:XMLSchema-instance="http://www.w3.org/2001/XMLSchema-instance" XMLSchema-instance:type="CanonicalUser">
            <ID>xxxx-xx</ID>
            <DisplayName>rahul.khan</DisplayName>
        </Grantee>
        <Permission>FULL_CONTROL</Permission>
    </Grant>
</AccessControlList>

因此,当文档被解码时,类型字段似乎没有被 aws-go-sdk 解析。

例如,这是我需要得到的未编码输出

Grantee: {
    DisplayName: "rahul.khan",
    ID: "xxxx-xx",
    Type: "CanonicalUser"
  },
  Permission: "FULL_CONTROL"
}

相反,我得到的是:

  Grantee: {
    DisplayName: "rahul.khan",
    ID: "xxxx-xx"
  },
  Permission: "FULL_CONTROL"
}

未编码的输出中似乎缺少 Type 属性。我在我的代码和 aws 生成的 xml 文档中看到的唯一区别是这一行

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:type="CanonicalUser" 对/秒

xmlns:XMLSchema-instance="http://www.w3.org/2001/XMLSchema-instance"XMLSchema-instance:type="CanonicalUser"

感谢是否有人可以帮助我了解如何解决此问题?

最佳答案

这个结构编码到你需要的 XML,只使用 endcoding/xml.Marshal():

type Grantee struct {
    Xmlns_xsi   string `xml:"xmlns:xsi,attr,omitempty"`  //set namespace prefix explicitly
    Xsi_type    string `xml:"xsi:type,attr,omitempty"`   //set namespace:attr  explicitly
    ID          string `xml:",omitempty"`
    DisplayName string `xml:",omitempty"`
}

g := Grantee{
    DisplayName: "rahul.khan",
    ID:          "xxxx-xx",
    Xsi_type:    "CanonicalUser",
    Xmlns_xsi:   "http://www.w3.org/2001/XMLSchema-instance", // set namespace URI as the value of the field
}

gm, err := xml.MarshalIndent(g, "", "  ")
if err != nil {
    fmt.Printf("error: %v", err)
    return
}

fmt.Printf("%+s\n", gm)

//<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
//  <ID>xxxx-xx</ID>
//  <DisplayName>rahul.khan</DisplayName>
//</Grantee>

https://play.golang.org/p/zrVlmktLyZu

请注意,xmlns:xsixsi:type 只是设置为显式属性名称,包括冒号。如您所见,如果在命名空间和属性名称之间留一个空格,encoding/xml 会进行更改以尝试清理输出中的命名空间(例如使用命名空间 URL 的最后一部分作为命名空间前缀:例如 XMLSchema-instance).

我知道这个 XML 可能不会按照您的意愿解码到您的 Go 结构中(正如许多 golang 问题中所讨论的那样,例如@Adrian 在对您的问题的评论中引用的问题)。但是,如果您要提交给 S3 Rest API 以更改 ACL,看起来这个 xml 是正确的)。 aws-go-sdk 似乎使用了他们的 own function反序列化 XML,所以请尝试一下。

关于xml - Go XML 编码的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53954457/

相关文章:

javascript - CDATA 真的有必要吗?

.net - 使用 XmlSerializer 将空的 xml 属性值反序列化为可为空的 int 属性

xml - 在本地托管 W3 XML 架构文件

docker - 如何让 vim 不要求确认交互?

go - 如何在不尝试更新其他模块的情况下将 'go get' 或 'go mod vendor' 用于特定模块?

json - JSON 的自定义编码(marshal)拆收器,可以是字符串或 map[string]string/map[string]bool

cocoa - NSTask字符串编码问题

servlets - 为什么带有空格的 cookie 值会带引号到达客户端?

java - 如何自动加载 servlet-context.xml?

java - 尝试读取系统提供给我的 listFiles 时出现 FileNotFoundException。这是名称编码问题吗?