英文:
how to golang Unmarshal a xml message into a struct
问题
如何解码这个 XML?
<?xml version="1.0" encoding="UTF-8"?>
<LocationConstraint>oss-cn-hangzhou</LocationConstraint>
我的代码如下:
type BucketLocation struct {
LocationConstraint string `xml:"LocationConstraint"`
}
v := &BucketLocation{}
xml.Unmarshal(xml_content, v)
但是它不起作用。
英文:
how to decode this xml
<?xml version="1.0" encoding="UTF-8"?>
<LocationConstraint>oss-cn-hangzhou</LocationConstraint>
my code is like this:
type BucketLocation struct {
LocationConstraint string `xml:"LocationConstraint"`
}
v := &BucketLocation{}
xml.Unmarshal(xml_content, v)
but it does not work.
答案1
得分: 3
你的结构体定义暗示了以下的 XML 格式,但与你提供的内容不匹配:
<BucketLocation>
<LocationConstraint>oss-cn-hangzhou</LocationConstraint>
</BucketLocation>
要读取你给出的示例 XML,你可以这样做:
var v string
xml.Unmarshal(xml_content, &v)
英文:
The definition of your struct implies the following XML format which doesn't match what you're providing:
<BucketLocation>
<LocationConstraint>oss-cn-hangzhou</LocationConstraint>
</BucketLocation>
To read the example XML that you've given you would do something this:
var v string
xml.Unmarshal(xml_content, &v)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论