2013-06-18 57 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 –

相关问题