无法正确地在Go中解组/组合动态XML结构

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

Unable to correctly unmarshal/marshal dynamic XML structs in Go

问题

我有一个描述XML模式的XSD文件,并且我正在尝试编写相应的Go结构体来实现基于该模式的XML的编组/解组。

这是我写的一些Go Playground代码,用于重现我遇到的问题。

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

在模式中,videoweb标签是动态的(即它们可以在media下出现多次,并且内部内容可能不同)。

我尝试编写一个动态的结构体,同时实现了Unmarshaller/Marshaller接口,就像示例中所示,我似乎能够进行解析,但它解组/编组内部内容时会忽略Name和URL列表,只取其中的一个元素。

我不确定问题出在哪里。

对此问题的任何帮助将不胜感激。

英文:

I have am xsd describing an XML schema and I am trying to write the representation of this xsd in go structures so I am able to marshal/unmarshal XML based on this schema.

Here is some go playground code I wrote to reproduce the problem I am having.

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

In the schema, the tags video and web are dynamic (as in they can both appear under media more than once and have different content inside).

I attempted writing a dynamic struct along with the Unmarshaller/Marshaller interface as the example shows and I seem to be able to perform the parsing but it unmarshals/marshals the internal content incorrect ignoring the Name and URL lists, only taking one element of each.

I am uncertain what the issue is here.

Any help on the matter is appreciated

答案1

得分: 1

我已经修改了你的示例代码,这里是修改后的代码:

Series(系列):

type Series struct {
    Name []Name `xml:"name"`
}

Website(网站):

type Website struct {
    Url []Url `xml:"url"`
}

请注意,我已经将 xml:"name"xml:"url" 修改为 xml:"name"xml:"url"

英文:

I have modified your sample here https://play.golang.org/p/rbcoL0ayeb. Change your definition to following:

Series:

type Series struct {
   Name []Name `xml:"name"`
}

Website:

type Website struct {
   Url []Url `xml:"url"`
}

huangapple
  • 本文由 发表于 2017年6月14日 06:55:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/44532848.html
匿名

发表评论

匿名网友

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

确定