2013-07-11 60 views
1

XML變型1:的EclipseLink MOXY:如何XML結構的變體匹配相同LBA模式

<root> 
    <elements> 
    <element /> 
    </elements> 
</root> 

XML變體2:

<root> 
    <element /> 
</root> 

的豆結構對於每個元素的類XML變體1,如圖所示相互嵌套。

期望的行爲是讓unmarshaller爲Variant 2創建與Variant 1相同的Bean。這意味着它應該創建一個Elements類,儘管它在結構中不存在。

下面是我使用的變體1的綁定: 「」

<xml-bindings 
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" 
package-name="demo"> 
<java-types> 
    <java-type name="Root"> 
     <xml-root-element name="root"/> 
     <java-attributes> 
      <xml-element java-attribute="elements" xml-path="elements" type="demo.Elements"/> 
     </java-attributes> 
    </java-type> 
    <java-type name="Elements"> 
     <java-attributes> 
      <xml-element java-attribute="element" xml-path="element" type="demo.Element" container-type="java.util.List"/> 
     </java-attributes> 
    </java-type> 
    <java-type name="Element" /> 
</java-types> 

我試圖適應XML路徑= 「元素」,以XML的路徑=並認爲這可能適用於變體2,但沒有成功。什麼是最簡單的方法來完成我想要的?

回答

1

您可以爲您的用例使用多個映射文件。

映射文件 - 變1

<?xml version="1.0"?> 
<xml-bindings 
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" 
    package-name="demo" 
    xml-accessor-type="FIELD"> 
    <java-types> 
     <java-type name="Root"> 
      <xml-root-element/> 
     </java-type> 
    </java-types> 
</xml-bindings> 

映射文件 - 變2

<?xml version="1.0"?> 
<xml-bindings 
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" 
    package-name="demo" 
    xml-accessor-type="FIELD"> 
    <java-types> 
     <java-type name="Root"> 
      <xml-root-element/> 
      <java-attributes> 
       <xml-element java-attribute="elements" xml-path="."/> 
      </java-attributes> 
     </java-type> 
    </java-types> 
</xml-bindings> 

演示

在演示代碼下面我們將創建兩個不同的實例JAXBContext爲具有不同元數據的相同域模型。

package demo; 

import java.io.File; 
import java.util.*; 
import javax.xml.bind.*; 
import org.eclipse.persistence.jaxb.JAXBContextProperties; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     // VARIANT #1 
     Map<String, Object> properties1 = new HashMap<String, Object>(1); 
     properties1.put(JAXBContextProperties.OXM_METADATA_SOURCE, "demo/oxm1.xml"); 
     JAXBContext jc1 = JAXBContext.newInstance(new Class[] {Root.class}, properties1); 
     Unmarshaller unmarshaller1 = jc1.createUnmarshaller(); 
     File variant1 = new File("src/demo/variant1.xml"); 
     Root root = (Root) unmarshaller1.unmarshal(variant1); 

     // VARIANT #2 
     Map<String, Object> properties2 = new HashMap<String, Object>(1); 
     properties2.put(JAXBContextProperties.OXM_METADATA_SOURCE, "demo/oxm2.xml"); 
     JAXBContext jc2 = JAXBContext.newInstance(new Class[] {Root.class}, properties2); 
     Marshaller marshaller2 = jc2.createMarshaller(); 
     marshaller2.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller2.marshal(root, System.out); 
    } 

} 
+0

偉大的工程!但是,你能告訴我爲什麼我的方法沒有?我沒有看到導致我不工作的差異。我做了/使用xml-path =「。」爲Variant 2輸入!? – hansi

+0

@hansi - 出於性能方面的考慮,MOXy嘗試對XML文檔進行單個深度優先遍歷。當它看到''「'''XPath時,它認爲該映射的條件滿足,並且不認爲可以應用其他選項,因爲它還沒有看到下一個元素。 –

+0

我看到了,謝謝 – hansi