Xstream 隐式映射作为根元素的属性 [英] Xstream Implicit Map As Attributes to Root Element

查看:29
本文介绍了Xstream 隐式映射作为根元素的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种使用 XStream 将地图转换为根元素属性的解决方案.

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.

我创建了一个自定义转换器并将其附加到根对象,然后在转换器中我可以访问我试图转换为属性的地图,我遍历地图并将属性写入节点,使用 writer.addAttribute(entry.getKey(), entry.getValue());这实际上将属性写入根节点,例如

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?

谢谢,乔恩

推荐答案

创建一个转换器,写出地图并回退到使用反射转换器编组对象:

Create a converter that writes out the map and falls back on marshalling the object using a reflection converter:

static class MyConverter implements Converter {

    private final Map<String, String> attributes;

    private final Class<?> clazz;

    private final Mapper mapper;

    private final ReflectionProvider reflectionProvider;

    public MyConverter(Mapper mapper,
            ReflectionProvider reflectionProvider, Class<?> clazz,
            Map<String, String> attributes) {
        super();
        this.mapper = mapper;
        this.reflectionProvider = reflectionProvider;
        this.attributes = attributes;
        this.clazz = clazz;
    }

    @Override
    public boolean canConvert(Class cls) {
        return cls == clazz;
    }

    @Override
    public void marshal(Object value, HierarchicalStreamWriter writer,
            MarshallingContext context) {
        for (String key : attributes.keySet()) {
            writer.addAttribute(key, attributes.get(key));
        }

        Converter converter = new ReflectionConverter(mapper,
                reflectionProvider);
        context.convertAnother(p, converter);
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader arg0,
            UnmarshallingContext arg1) {
        // TODO Auto-generated method stub
        return null;
    }

}

从您的 XStream 实例中检索 Mapper 和 ReflectionProvider 实例,并注册一个具有所有必要设置的转换器:

Retrieve the Mapper and ReflectionProvider instances from your XStream instance, and register a converter with all necessary setup:

    XStream xs = new XStream(new DomDriver());
    Mapper mapper = xs.getMapper();
    ReflectionProvider reflectionProvider = xs.getReflectionProvider();
    xs.alias("youralias", YourRoot.class);
    xs.registerConverter(new MyConverter(mapper, reflectionProvider,
            YourRoot.class, map));

    System.out.println(xs.toXML(yourRoot));

这篇关于Xstream 隐式映射作为根元素的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆