2014-04-25 118 views
1

我有ParentChild類。JaxB,Eclipse Moxy,polymorhpic映射

A House類的字段類型爲Parent,它可以引用Child對象。我需要使用Eclipse Moxy將其映射到XML。

其XSD會是這樣的:

<xs:complexType name="Parent" abstract="true"> ...other fields...

<xs:complexType name="Child" > <xs:extension base="Parent"> ...other fields...

<xs:element name="child" type="Child" substitutionGroup="parent" /> <xs:element name="parent" type="Parent" abstract="true" />

<xs:complexType name="House"> <xs:element ref="parent"/>

衆議院類包含一個JAXBElement的指向家長: @XmlElementRef(name = "parent", namespace = "abc", type = JAXBElement.class) protected JAXBElement<? extends Parent> parent;

如何通過House.oxm.xml文件映射House類,以便此多態映射能夠正確工作?

<java-type name="House" xml-accessor-type="NONE"> <java-attributes> <xml-element-ref java-attribute="?????????"/>

我試圖映射使用「@」,但它不工作 - 它只是將對象添加到XML的參考字符串(@Parent)。

回答

1

整個問題是因爲:

@XmlElementRef(name = "parent", namespace = "abc", type = JAXBElement.class) protected JAXBElement<? extends Parent> parent;

經過多次徒勞的嘗試修復它,我碰到這個缺陷來了:https://bugs.eclipse.org/bugs/show_bug.cgi?id=327811

指的是其代碼後,我看到它解決這與我的情況非常相似,只是在超類型上使用了@XmlElementRef註釋,而沒有使用任何其他類似JAXBElement或將任何其他參數提供給@XmlElementRef。

我試過了(並從oxm文件中刪除了它的映射),它的工作方式就像一個魅力!我希望這個答案能夠幫助那些陷入同樣問題的人。