2013-06-18 51 views
1

我有一個相當大的對象樹,我想要導出到XML。一個名爲Person的對象在幾個地方使用(作爲userCreated,許多子實體的userModified,作爲客戶端等)JAXB/MOXy對多個屬性使用相同的實體類型

根據Person對象的用法,我需要在xml中有不同的元素。例如:

<policy> 
    <userCreated> 
    <firstName>John</firstName> 
    <lastName>Doe</lastName> 
    </userCreated> 
    <client> 
    <clientId>1234</clientId> 
    <email>[email protected]</email> 
    <firstName>John</firstName> 
    <lastName>Doe</lastName> 
    </client> 
</policy> 

userCreated和客戶端在同一個對象的實例(名爲Person)

怎麼能在這個被bindings.xml成立?

+0

的'Person'類具有'clientId','email','firstName','lastName'並基於該對象似乎要限制哪些屬性編組到XML? –

+0

是的,這正是我需要的 – yglodt

回答

0

您可以使用EclipseLink JAXB (MOXy)@XmlNamedObjectGraph擴展名來支持此用例。 @XmlNamedObjectGraph允許您執行的操作是在您的數據上創建多個視圖。

下面我們將用@XmlNamedObjectGraph創建的Person類只公開2場(firstNamelastName)的景色。

import javax.xml.bind.annotation.*; 
import org.eclipse.persistence.oxm.annotations.*; 

@XmlNamedObjectGraph(
    name = "simple", 
    attributeNodes = { 
     @XmlNamedAttributeNode("firstName"), 
     @XmlNamedAttributeNode("lastName") 
    } 
) 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Person { 

    private int clientId; 
    private String firstName; 
    private String lastName; 
    private String email; 

    public void setClientId(int clientId) { 
     this.clientId = clientId; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

} 

政策

我們也將在Policy類使用@XmlNamedObjectGraph。它說,對於userCreated字段,應用我們在Person類中定義的名爲simple的命名對象圖。

import javax.xml.bind.annotation.*; 
import org.eclipse.persistence.oxm.annotations.*; 

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlNamedObjectGraph(
    name = "policy", 
    attributeNodes = { 
     @XmlNamedAttributeNode(value = "userCreated", subgraph = "simple"), 
     @XmlNamedAttributeNode("client") 
    } 
) 
public class Policy { 

    private Person userCreated; 
    private Person client; 

    public void setUserCreated(Person userCreated) { 
     this.userCreated = userCreated; 
    } 

    public void setClient(Person client) { 
     this.client = client; 
    } 

} 

演示

在演示代碼下面我們將指定我們想要在Marshaller使用MarshallerProperties.OBJECT_GRAPH屬性應用的命名對象圖。

import javax.xml.bind.*; 
import org.eclipse.persistence.jaxb.MarshallerProperties; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(Policy.class); 

     Person person = new Person(); 
     person.setClientId(1234); 
     person.setFirstName("John"); 
     person.setLastName("Doe"); 
     person.setEmail("[email protected]"); 

     Policy policy = new Policy(); 
     policy.setClient(person); 
     policy.setUserCreated(person); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, "policy"); 
     marshaller.marshal(policy, System.out); 
    } 

} 

輸出

下面是從運行演示代碼的輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<policy> 
    <userCreated> 
     <firstName>John</firstName> 
     <lastName>Doe</lastName> 
    </userCreated> 
    <client> 
     <clientId>1234</clientId> 
     <firstName>John</firstName> 
     <lastName>Doe</lastName> 
     <email>[email protected]</email> 
    </client> 
</policy> 

更多信息

+0

非常感謝您的詳細和有益的職位!由於我使用外部綁定文檔,我不知道是否可以爲我的Person對象設置多個xml-named-object-graph,並且從(或者更好的從)?我想避免註釋並僅僅使用外部綁定文檔。另外,我想盡可能地在外部綁定文檔中進行配置,並儘可能少地從Java中進行配置。 – yglodt

+0

@yglodt - 以下鏈接有一個在外部映射文檔中配置命名對象圖的示例:http://blog.bdoughan.com/2013/03/moxys-object-graphs-inputoutput-partial.html –

相關問題