English 中文(简体)
十流 混凝土地图
原标题:Xstream Implicit Map As Attributes to Root Element

我正试图找到一种解决办法,利用XStream将地图转化为根要素。

我不认为这是不可能的,但我已经努力了。

我创建了一个习俗转换器,并把它附在根子上,然后,在转换器中,我获得我试图转换成属性的地图,我通过地图敲响,并用作者写给 no子。 添加Attribute(条目.getKey(),条目.getValue();这实际上写上了根子的特性,例如:

这种做法的问题是,它没有处理该文件的其余部分,只是处理地图后才停止,以便让我工作,我需要某种方法,让违约的兑换者重新控制并填写模型。

我试图使用的第二个解决办法是,仅仅为了地图本身而创建一种习俗转换器,而这一办法的问题是,我无法处理根本内容,因此我无法给它写信,能否以这种方式获取根本内容?

Thanks, Jon

问题回答

1. 创建出地图的转换器,然后使用反射转换器对物体进行清点:

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 案的“地图片”和“反射”事例,并登记一个拥有一切必要装置的转换器:

    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));




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签