英文:
Unable to correctly unmarshal/marshal dynamic XML structs in Go
问题
我有一个描述XML模式的XSD文件,并且我正在尝试编写相应的Go结构体来实现基于该模式的XML的编组/解组。
这是我写的一些Go Playground代码,用于重现我遇到的问题。
https://play.golang.org/p/ktJOsCDyLW
在模式中,video
和web
标签是动态的(即它们可以在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"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论