2012-01-13 100 views
2

使用的EclipseLink/MOXY 2.3我已经在编组到XML以下用例:JAXB-的EclipseLink:XmlRootElement将和继承

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> 

的问题是:我如何实现这一目标?莫西只是忽略我的MyImpl XmlRootElement将...

编辑:

试图布莱斯什么建议给了我以下异常:

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)的映射中添加了type-attribute。

因此,使用xml-element-ref MOXy不再“看到”吸气剂,使用xml-element它。

+0

A类是一种接口? A映射到XML的具体实现是什么? – 2012-01-16 13:18:31

+0

@BlaiseDoughan:我没有针对具体类的映射 - 只是接口bar.A的映射。由于我只是通过界面传递,我认为仅仅为界面创建映射就足够了。 我遵循http://blog.bdoughan.com/2011/05/jaxb-and-interface-fronted-models.html中的第一个示例。 – quaylar 2012-01-16 13:33:22

+0

在这个例子中,假设接口'A'对应于我的例子中的'Customer'接口,在我的例子中,您仍然需要引导并映射JAXBContext到impl类,即CustomerImpl。 – 2012-01-16 13:46:10

回答

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> 

富/ OXM的XML表示。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> 

更多信息

+1

感谢支持莫西标签;) – 2012-01-14 00:06:56

+0

布莱斯感谢您的提示,我实际上已经在发布之前尝试过(根据关于此主题的博客文章),但在编组期间发生异常。我会用一些额外的信息编辑我的帖子。 – quaylar 2012-01-16 12:04:49

+0

@quaylar - 在你的更新中,你可以包括你看到的异常吗? – 2012-01-16 12:06:51