给子元素添加属性而不生成新的 XML 元素。

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

Add attribute to the child without generating new XML element

问题

如何使用Go XML包创建这个XML呢?

  1. <Files><File width="200" height="100">somevaluehere</File></Files>

这比你想象的要困难,例如:

  1. type Video struct {
  2. Files Files `xml:"Files"`
  3. }
  4. type Files struct {
  5. Width string `xml:"Width,attr"`
  6. Height string `xml:"Height,attr"`
  7. File string `xml:",chardata"`
  8. }

输出结果是:

  1. <Video><Files Width="640" Height="480"><File>somevalue</File></Files></Video>

而我想要的是:

  1. <Video><Files><File Width="640" Height="480">somevalue</File></Files></Video>
英文:

How can I create this XML using the Go XML package?

  1. &lt;Files&gt;&lt;File width=&quot;200&quot; height=&quot;100&quot;&gt;somevaluehere&lt;/File&gt;&lt;/Files&gt;

It is harder than you may think, for example:

  1. type Video struct {
  2. Files Files `xml:&quot;Files&quot;`
  3. }
  4. type Files struct {
  5. Width string `xml:&quot;Width,attr&quot;`
  6. Height string `xml:&quot;Height,attr&quot;`
  7. File string `xml:&quot;File&quot;`
  8. }

the output is:

  1. &lt;Video&gt;&lt;Files Width=&quot;640&quot; Height=&quot;480&quot;&gt;&lt;File&gt;somevalue&lt;/File&gt;&lt;/Files&gt;&lt;/Video&gt;

And I want:

  1. &lt;Video&gt;&lt;Files&gt;&lt;File Width=&quot;640&quot; Height=&quot;480&quot;&gt;somevalue&lt;/File&gt;&lt;/Files&gt;&lt;/Video&gt;

答案1

得分: 2

你想要将Video.Files渲染为<Video><Files><File>标签,因此在标签中指明这一点:

  1. type Video struct {
  2. Files Files `xml:"Files>File"`
  3. }
  4. type Files struct {
  5. Width string `xml:"Width,attr"`
  6. Height string `xml:"Height,attr"`
  7. File string `xml:",chardata"`
  8. }

并且你希望Files.File的值成为标签的字符数据,也在标签中指明这一点:

  1. type Files struct {
  2. Width string `xml:"Width,attr"`
  3. Height string `xml:"Height,attr"`
  4. File string `xml:",chardata"`
  5. }

进行测试:

  1. v := Video{
  2. Files: Files{
  3. Width: "640",
  4. Height: "480",
  5. File: "someValue",
  6. },
  7. }
  8. out, err := xml.Marshal(v)
  9. if err != nil {
  10. panic(err)
  11. }
  12. fmt.Println(string(out))

输出结果为:

  1. <Video><Files><File Width="640" Height="480">someValue</File></Files></Video>

如果<Files>内部可能有多个<File>元素,我会将Video.Files定义为切片:

  1. type Video struct {
  2. Files []File `xml:"Files>File"`
  3. }
  4. type File struct {
  5. Width string `xml:"Width,attr"`
  6. Height string `xml:"Height,attr"`
  7. File string `xml:",chardata"`
  8. }

进行测试:

  1. v := Video{
  2. Files: []File{
  3. {
  4. Width: "640",
  5. Height: "480",
  6. File: "someValue",
  7. },
  8. {
  9. Width: "320",
  10. Height: "240",
  11. File: "otherValue",
  12. },
  13. },
  14. }
  15. out, err := xml.Marshal(v)
  16. if err != nil {
  17. panic(err)
  18. }
  19. fmt.Println(string(out))

输出结果为:

  1. <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 &lt;Video&gt;&lt;Files&gt;&lt;File&gt; tag, so indicate this in the tag:

  1. type Video struct {
  2. Files Files `xml:&quot;Files&gt;File&quot;`
  3. }

And you want the Files.File value to be the tag's char data, also indicate this in the tag:

  1. type Files struct {
  2. Width string `xml:&quot;Width,attr&quot;`
  3. Height string `xml:&quot;Height,attr&quot;`
  4. File string `xml:&quot;,chardata&quot;`
  5. }

Testing it:

  1. v := Video{
  2. Files: Files{
  3. Width: &quot;640&quot;,
  4. Height: &quot;480&quot;,
  5. File: &quot;someValue&quot;,
  6. },
  7. }
  8. out, err := xml.Marshal(v)
  9. if err != nil {
  10. panic(err)
  11. }
  12. fmt.Println(string(out))

Which outputs (try it on the Go Playground):

  1. &lt;Video&gt;&lt;Files&gt;&lt;File Width=&quot;640&quot; Height=&quot;480&quot;&gt;someValue&lt;/File&gt;&lt;/Files&gt;&lt;/Video&gt;

If there could me multiple &lt;File&gt; elements inside &lt;Files&gt;, I would make Video.Files a slice:

  1. type Video struct {
  2. Files []File `xml:&quot;Files&gt;File&quot;`
  3. }
  4. type File struct {
  5. Width string `xml:&quot;Width,attr&quot;`
  6. Height string `xml:&quot;Height,attr&quot;`
  7. File string `xml:&quot;,chardata&quot;`
  8. }

Testing it:

  1. v := Video{
  2. Files: []File{
  3. {
  4. Width: &quot;640&quot;,
  5. Height: &quot;480&quot;,
  6. File: &quot;someValue&quot;,
  7. },
  8. {
  9. Width: &quot;320&quot;,
  10. Height: &quot;240&quot;,
  11. File: &quot;otherValue&quot;,
  12. },
  13. },
  14. }
  15. out, err := xml.Marshal(v)
  16. if err != nil {
  17. panic(err)
  18. }
  19. fmt.Println(string(out))

Which outputs (try it on the Go Playground):

  1. &lt;Video&gt;&lt;Files&gt;&lt;File Width=&quot;640&quot; Height=&quot;480&quot;&gt;someValue&lt;/File&gt;&lt;File Width=&quot;320&quot; Height=&quot;240&quot;&gt;otherValue&lt;/File&gt;&lt;/Files&gt;&lt;/Video&gt;

huangapple
  • 本文由 发表于 2022年2月3日 20:17:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/70971154.html
匿名

发表评论

匿名网友

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

确定