2018-07-20 22:08
浏览 385

Go中具有动态类型(空接口)的XML Unmarshal

I need to parse an XML message that has a dynamic element, so I have used an element of type interface{} to represent it in my Message struct.

Once I know the type of this dynamic element (at runtime) I initialise a message struct and then try to unmarshal the XML message. However, the contents of the dynamic element are not unmarshalled.

Here is a Go Playground with what I'm trying to achieve, with comments and the actual vs expected outputs:

I tried several variations but can't get the unmarshalling to work as expected. Can anyone help me to understand why this behaviour and how to make it work? Thanks in advance.

图片转代码服务由CSDN问答提供 功能建议

我需要解析具有动态元素的XML消息,因此我使用了类型的元素 interface {} </ code>表示在我的Message结构中。 </ p>

一旦知道了此动态元素的类型(在运行时),我将初始化消息结构,然后尝试解组XML消息。 但是,动态元素的内容并未编组。</ p>

这是一个Go Playground,其中包含我要实现的目标,注释以及实际与预期的输出: </ p>

我尝试了几种变体,但无法按预期工作。 谁能帮助我了解为什么这种行为以及如何使其起作用? 预先感谢。</ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题