2012-01-13 101 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