jaxb2 - 使用适配器将类编码到具有 MOXy 或任何其他 JAXB 实现的根元素

标签 jaxb2 moxy

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

我已经尝试对下面的内容使用多种变体,但仍然受阻。当我创建 JAXB 上下文时,我可以看到我的适配器类被加载和实例化,但是当我编码配置对象时它永远不会被调用。

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

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 映射文件:
<?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到正在编码的根对象。您可以在执行编码之前或执行解码之后显式调用适配器逻辑。

关于jaxb2 - 使用适配器将类编码到具有 MOXy 或任何其他 JAXB 实现的根元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21640566/

相关文章:

jaxb2 - 如何使用 JAXB 对不同模式应用不同的自定义

jaxb - 在 xml 中隐藏一个只有在解码时才能检索到的值

java - Spring MVC - 使用 @ResponseBody 时设置 JAXB 编码器属性

moxy - 使用 Jersey 2.x/MOXy 自定义 JSON 序列化

java - EclipseLink 动态 MOXy 访问枚举值

java - 使用 EclipseLink MOXy 读取同一元素两次

ant - schemagen ant 任务忽略 package-info.java

java - 如何在命令提示符下使用 xjc 从 WSDL 使用 @XmlRootElement 生成 java 类

maven-2 - Maven 中同一项目的 maven-jaxb2-plugin 插曲,可能吗?

java - 使用 JAXB&MOXy 将 JPA 实体转换为 XML 并再次转换回来