2

使用 Eclipselink/MOXy 2.3 我在编组到 XML 时有以下用例:

abstract class MyAbstract {
}

class MyImpl extends MyAbstract {
}

class A {

    private MyAbstract myAbstract;

    // MyImpl is behind this
    public MyAbstract getMyAbstract() {
        return myAbstract;
    }

}

我在 oxm.xml 中定义了以下映射:

<java-type name="foo.MyAbstract" xml-accessor-type="NONE">
    <xml-see-also>
        foo.MyImpl
    </xml-see-also>
</java-type>

<java-type name="foo.MyImpl">
    <xml-root-element name="MyImpl" />
</java-type>

<java-type name="bar.A" xml-accessor-type="NONE">
    <xml-root-element name="A" />
    <java-attributes>
        <xml-element java-attribute="myAbstract" type="foo.MyAbstract" />
    </java-attributes>
</java-type>

现在这导致:

<A>
    <myAbstract xsi:type="myImpl">
        <!-- Mapped members of MyImpl + MyAbstract -->
    </myAbstract>
</A>

因为我不想要导出的 xml 中的属性名称,所以我更改了:

<java-type name="bar.A" xml-accessor-type="NONE">
    <xml-root-element name="A" />
    <java-attributes>
        <xml-element java-attribute="myAbstract" type="foo.MyAbstract" xml-path="."/>
    </java-attributes>
</java-type>

这导致:

<A>
    <!-- Members of MyImpl + MyAbstract marshalled without any wrapping element-->
</A>

我想要的是:

 <A>
    <MyImpl>
        <!-- Members of MyImpl + MyAbstract -->
    </MyImpl>
 </A>

问题是:我如何实现这一目标?MOXy 只是忽略了我在 MyImpl 上的 XmlRootElement ......

编辑:

尝试 Blaise 的建议会给我以下异常:

Exception [EclipseLink-60] (Eclipse Persistence Services - 2.3.2.v20111125-r10461):   
org.eclipse.persistence.exceptions.DescriptorException
The method [] or [getMyAbstract] is not defined in the object [bar.A].

现在这需要我之前遗漏的更多信息,因为我认为它不相关:

A 类是一个接口,它定义:public X getMyAbstract(); MyAbstract 实现 X(这就是我在接口 A 的映射中添加类型属性的原因)。

所以,使用xml-element-refMOXy 不再“看到”吸气剂,使用xml-element它。

4

1 回答 1

2

您正在寻找的映射是@XmlElementRef. 这对应于 XML 模式中的替换组的概念。

酒吧/oxm.xml

下面是bar包的外部映射文档。请注意 myAbstract 属性是如何映射的,xml-element-ref它是@XmlElementRef

<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="bar">
    <java-types>
        <java-type name="A" xml-accessor-type="NONE">
            <xml-root-element name="A" />
            <java-attributes>
                <xml-element-ref java-attribute="myAbstract"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

foo/oxm.xml

以下是foo包的外部元数据文件:

<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="foo">
    <java-types>
        <java-type name="MyAbstract" xml-accessor-type="NONE">
            <xml-see-also>
                foo.MyImpl
            </xml-see-also>
        </java-type>
        <java-type name="MyImpl">
            <xml-root-element name="MyImpl" />
        </java-type>
    </java-types>
</xml-bindings>

演示

以下是此示例的演示代码:

package forum8853855;

import java.util.*;
import javax.xml.bind.*;    
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import bar.A;
import foo.MyImpl;

public class Demo {

    public static void main(String[] args) throws Exception {
        List<String> oxm = new ArrayList<String>(2);
        oxm.add("foo/oxm.xml");
        oxm.add("bar/oxm.xml");

        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, oxm);

        JAXBContext jc = JAXBContext.newInstance(new Class[] {A.class}, properties);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        A a = new A();
        a.setMyAbstract(new MyImpl());
        marshaller.marshal(a, System.out);
    }

}

输出

<?xml version="1.0" encoding="UTF-8"?>
<A>
   <MyImpl/>
</A>

了解更多信息

于 2012-01-13T20:14:02.143 回答