Xstream 隐式映射作为根元素的属性
我正在尝试找到一种使用 XStream 将地图转换为根元素属性的解决方案。
我认为这是不可能的,但这是我迄今为止所尝试的。
我创建了一个自定义转换器并将其附加到根对象,然后在转换器中我可以访问我试图转换为属性的地图,我迭代地图并将属性写入节点,使用 writer.addAttribute(条目.getKey(), 条目.getValue());这实际上将属性写入根节点,例如
这种方法的问题是它不处理文档的其余部分,它只是在处理地图后停止,为了使其正常工作,我需要某种方法让默认转换器重新获得控制并完成模型。
我一直尝试使用的第二个解决方案是仅为地图本身创建一个自定义转换器,这种方法的问题是我无法获取根元素的句柄,因此我无法写入它,是否可能以这种方式访问根元素?
谢谢, 乔恩
I am trying to find a solution to convert a map into root element attributes using XStream.
I do not think this is possible but here is what I have tried to far.
I created a custom converter and attached it to the root object, in the converter I then get access to the map that I am trying to convert into attributes, I iterate through the map and write the attirbute to the node, using writer.addAttribute(entry.getKey(), entry.getValue()); this does actually write the attributes to the root node e.g.
The problem with this approach is that it does not process the rest of the document, it just stops after processing the map, in order to get this to work I need some method of letting the default converter back in control and finish off the model.
The second solution I have been trying to use is to create a custom converter just for the map its self, the problem with this approach is that I can not get a handle to the root element so I can not write to it, is it possible to access the root element this way?
Thanks,
Jon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个转换器,用于写出映射并使用反射转换器来编组对象:
从 XStream 实例中检索 Mapper 和 ReflectionProvider 实例,并使用所有必要的设置注册转换器:
Create a converter that writes out the map and falls back on marshalling the object using a reflection converter:
Retrieve the Mapper and ReflectionProvider instances from your XStream instance, and register a converter with all necessary setup: