Groovy XML映射分组重复键
我有一个XML响应,希望将其转换为地图,但是某些XML节点是重复,因此我希望将它们转换为 地图列表。 目前,我正在使用此文章中建议的此代码: xmlslslurper-to-to-return-to-return-to-return-to-return-all-xml-all-xml-- a-map
预先感谢。
示例:
<head>test</head>
<tail>
<name>1</name>
<name>2</name>
</tail>
</body>
我想要以下地图:
["head" : "test" , "tail" : [["name":"1"],["name":"2"]]]
I have a xml response and i want it to be converted to a map but some xml nodes are duplicate so i want those to be converted to List of maps.
Currently I'm using this code suggested in this post :
xmlslurper-to-return-all-xml-elements-into-a-map
Thanks in advance.
Sample :
<head>test</head>
<tail>
<name>1</name>
<name>2</name>
</tail>
</body>
and I want the following map :
["head" : "test" , "tail" : [["name":"1"],["name":"2"]]]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于该代码:
覆盖所得地图中的值。我没有设法在不做一些丑陋的黑客的情况下找到优雅的解决方案。但这是我的解决方案:
我在这里做的是我首先收集所有孩子作为地图条目列表,因此看起来像
[[key1:value1],[key2:value2]]
。然后,我循环浏览此中间结构并收集结果。我希望它有助于前进。也许以后有人会以更好的解决方案来找您,因为正如我所说,目前我还没有找到任何优雅的方法来解决它。
The problem is that this piece of code:
overrides the value in the resulting map. I didn't manage to find an elegant solution to it without doing some ugly hacks. But here is my solution:
What I'm doing here is that I first collect all the children as list of map entries, so it looks like
[[key1:value1], [key2:value2]]
. Then I loop over this intermediate structure and gather the results.I hope it helps to move forward. Maybe later someone will come to you with a better solution, because as I said, at the moment I haven't found any elegant way to solve it.
经过一些挣扎之后,我编写了此代码来解决我的问题,我尝试使用MultivalUemap,但它正在转换所有值列出,因此我最终必须自己写作:
After some struggling I wrote this code to solve my problem, I tried to use MultiValueMap either but it was converting all the values to list so Finally I had to write in on my own :