使用适配器通过 MOXy 或任何其他 JAXB 实现将类编组到根元素 [英] Using an adapter to marshal a class to a root element with MOXy or any other JAXB implementation

查看:21
本文介绍了使用适配器通过 MOXy 或任何其他 JAXB 实现将类编组到根元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 Apache Commons Configuration 扩展 CompositeConfiguration 类的类.我正在尝试使用 MOXy 将其编组为 XML.我创建了一个 XML 适配器,它将配置转换为简单的名称/值对象列表.

I have a class which extends the CompositeConfiguration class from Apache Commons Configuration. I am trying to marshal it to XML using MOXy. I have created an XML adapter that converts the configuration to a list of simple name/value objects.

我已尝试对下面的内容使用多种变体,但仍然受阻.当我创建 JAXB 上下文时,我可以看到我的适配器类正在加载和实例化,但在我编组配置对象时它从未被调用.

I have tried using a number of variations on what I have below but am still stymied. I can see my adapter class being loaded and instantiated when I create the JAXB context but it is never called when I marshal the configuration object.

查看 MOXy 源代码,我开始怀疑不可能为也是根元素的 Java 类指定 XML 适配器.我是否走在正确的轨道上,或者是否有完全不同的方法来做到这一点?

Looking at the MOXy source code, I am beginning to suspect that it is impossible to specify an XML adapter for a Java class that is also a root element. Am I on the right track to getting this working or is there a completely different way to do this?

XML 适配器:

public class JAXBConfigurationAdapter extends XmlAdapter<Object, BetterBaseConfiguration>
{

    @Override
    public Object marshal(final BetterBaseConfiguration javaObject) throws Exception
    {
        List<ConfigurationPropertyXmlType> jaxbConfiguration = new ArrayList<>();
        Iterator<String> keys = javaObject.getKeys();
        while (keys.hasNext())
        {
            String key = keys.next();
            ConfigurationPropertyXmlType property = new ConfigurationPropertyXmlType();
            property.setKey(key);
            property.setValue(javaObject.getString(key));
            jaxbConfiguration.add(property);
        }

        return jaxbConfiguration;
    }

    @Override
    public CompositeConfiguration unmarshal(final Object jaxbObject) throws Exception
    {
        BetterBaseConfiguration configuration = new BetterBaseConfiguration();
        for (ConfigurationPropertyXmlType property : (List<ConfigurationPropertyXmlType>) jaxbObject)
        {
            configuration.setProperty(property.getKey(), property.getValue());
        }

        return configuration;
    }

}

名称/值类:

public class ConfigurationPropertyXmlType
{
    private String key;
    private String value;

    public String getKey()
    {
        return key;
    }

    public String getValue()
    {
        return value;
    }

    public void setKey(final String key)
    {
        this.key = key;
    }

    public void setValue(final String value)
    {
        this.value = value;
    }
}

EclipseLink 映射文件:

EclipseLink mapping file:

<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings version="2.5" xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
        http://www.eclipse.org/eclipselink/xsds/persistence/oxm http://www.eclipse.org/eclipselink/xsds/eclipselink_oxm_2_5.xsd"
    package-name="myapp.configuration">

    <xml-schema element-form-default="QUALIFIED">
        <xml-ns prefix="mynm" namespace-uri="http://mynamespace" />
    </xml-schema>

    <java-types>
        <java-type name="myapp.configuration.BetterBaseConfiguration" >
            <xml-java-type-adapter value="myapp.configuration.JAXBConfigurationAdapter" type="myapp.configuration.BetterBaseConfiguration" />
            <xml-root-element name="Configuration" />
        </java-type>
    </java-types>

</xml-bindings>

期望的输出:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property>
        <name>some name</name>
        <value>some value</value>
    </property>
</configuration>

推荐答案

EclipseLink MOXy 和其他 JAXB (JSR-222) 提供者不会将 XmlAdapter 应用于正在编组的根对象.您可以在执行编组之前或执行解组之后显式调用适配器逻辑.

EclipseLink MOXy and other JAXB (JSR-222) providers do not apply an XmlAdapter to the root object being marshalled. You can explicitly call the adapter logic before doing a marshal or after doing an unmarshal.

这篇关于使用适配器通过 MOXy 或任何其他 JAXB 实现将类编组到根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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