英文:
Add attribute to the child without generating new XML element
问题
如何使用Go XML包创建这个XML呢?
<Files><File width="200" height="100">somevaluehere</File></Files>
这比你想象的要困难,例如:
type Video struct {
Files Files `xml:"Files"`
}
type Files struct {
Width string `xml:"Width,attr"`
Height string `xml:"Height,attr"`
File string `xml:",chardata"`
}
输出结果是:
<Video><Files Width="640" Height="480"><File>somevalue</File></Files></Video>
而我想要的是:
<Video><Files><File Width="640" Height="480">somevalue</File></Files></Video>
英文:
How can I create this XML using the Go XML package?
<Files><File width="200" height="100">somevaluehere</File></Files>
It is harder than you may think, for example:
type Video struct {
Files Files `xml:"Files"`
}
type Files struct {
Width string `xml:"Width,attr"`
Height string `xml:"Height,attr"`
File string `xml:"File"`
}
the output is:
<Video><Files Width="640" Height="480"><File>somevalue</File></Files></Video>
And I want:
<Video><Files><File Width="640" Height="480">somevalue</File></Files></Video>
答案1
得分: 2
你想要将Video.Files
渲染为<Video><Files><File>
标签,因此在标签中指明这一点:
type Video struct {
Files Files `xml:"Files>File"`
}
type Files struct {
Width string `xml:"Width,attr"`
Height string `xml:"Height,attr"`
File string `xml:",chardata"`
}
并且你希望Files.File
的值成为标签的字符数据,也在标签中指明这一点:
type Files struct {
Width string `xml:"Width,attr"`
Height string `xml:"Height,attr"`
File string `xml:",chardata"`
}
进行测试:
v := Video{
Files: Files{
Width: "640",
Height: "480",
File: "someValue",
},
}
out, err := xml.Marshal(v)
if err != nil {
panic(err)
}
fmt.Println(string(out))
输出结果为:
<Video><Files><File Width="640" Height="480">someValue</File></Files></Video>
如果<Files>
内部可能有多个<File>
元素,我会将Video.Files
定义为切片:
type Video struct {
Files []File `xml:"Files>File"`
}
type File struct {
Width string `xml:"Width,attr"`
Height string `xml:"Height,attr"`
File string `xml:",chardata"`
}
进行测试:
v := Video{
Files: []File{
{
Width: "640",
Height: "480",
File: "someValue",
},
{
Width: "320",
Height: "240",
File: "otherValue",
},
},
}
out, err := xml.Marshal(v)
if err != nil {
panic(err)
}
fmt.Println(string(out))
输出结果为:
<Video><Files><File Width="640" Height="480">someValue</File><File Width="320" Height="240">otherValue</File></Files></Video>
英文:
You want Video.Files
to be rendered into <Video><Files><File>
tag, so indicate this in the tag:
type Video struct {
Files Files `xml:"Files>File"`
}
And you want the Files.File
value to be the tag's char data, also indicate this in the tag:
type Files struct {
Width string `xml:"Width,attr"`
Height string `xml:"Height,attr"`
File string `xml:",chardata"`
}
Testing it:
v := Video{
Files: Files{
Width: "640",
Height: "480",
File: "someValue",
},
}
out, err := xml.Marshal(v)
if err != nil {
panic(err)
}
fmt.Println(string(out))
Which outputs (try it on the Go Playground):
<Video><Files><File Width="640" Height="480">someValue</File></Files></Video>
If there could me multiple <File>
elements inside <Files>
, I would make Video.Files
a slice:
type Video struct {
Files []File `xml:"Files>File"`
}
type File struct {
Width string `xml:"Width,attr"`
Height string `xml:"Height,attr"`
File string `xml:",chardata"`
}
Testing it:
v := Video{
Files: []File{
{
Width: "640",
Height: "480",
File: "someValue",
},
{
Width: "320",
Height: "240",
File: "otherValue",
},
},
}
out, err := xml.Marshal(v)
if err != nil {
panic(err)
}
fmt.Println(string(out))
Which outputs (try it on the Go Playground):
<Video><Files><File Width="640" Height="480">someValue</File><File Width="320" Height="240">otherValue</File></Files></Video>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论