Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JAXB/MOXy对多个属性使用相同的实体类型_Java_Xml_Jaxb_Moxy - Fatal编程技术网

Java JAXB/MOXy对多个属性使用相同的实体类型

Java JAXB/MOXy对多个属性使用相同的实体类型,java,xml,jaxb,moxy,Java,Xml,Jaxb,Moxy,我有一个相当大的对象树,我想将其导出为XML。名为Person的对象在多个位置使用(如userCreated、userModified或client等) 根据Person对象的使用情况,我需要在xml中包含不同的元素。例如: <policy> <userCreated> <firstName>John</firstName> <lastName>Doe</lastName> </userCrea

我有一个相当大的对象树,我想将其导出为XML。名为Person的对象在多个位置使用(如userCreated、userModified或client等)

根据Person对象的使用情况,我需要在xml中包含不同的元素。例如:

<policy>
  <userCreated>
    <firstName>John</firstName>
    <lastName>Doe</lastName>
  </userCreated>
  <client>
    <clientId>1234</clientId>
    <email>jdoe@example.com</email>
    <firstName>John</firstName>
    <lastName>Doe</lastName>
  </client>
</policy>

约翰
雌鹿
1234
jdoe@example.com
约翰
雌鹿
userCreated和client是同一对象(命名的人)的实例

如何在bindings.xml中进行设置?

您可以使用的
@XmlNamedObjectGraph
扩展来支持此用例。
@XmlNamedObjectGraph
允许您在数据上创建多个视图

下面我们将使用
@XmlNamedObjectGraph
Person
类上创建一个视图,该视图仅公开2个字段(
firstName
lastName

政策

我们还将在
策略
类上使用
@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;
    }

}
演示

在下面的演示代码中,我们将使用
MarshallerProperties.object\u graph
属性指定要应用于
Marshaller
的命名对象图

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("jdoe@example.com");

        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>jdoe@example.com</email>
   </client>
</policy>

约翰
雌鹿
1234
约翰
雌鹿
jdoe@example.com
了解更多信息


  • Person
    类有属性
    clientId
    email
    ,`firstName
    lastName`并且基于对象出现的位置,您想限制哪些属性被封送到XML?是的,这正是我需要的,非常感谢您的详细和有用的帖子!由于我使用了一个外部绑定文档,我想知道是否可以为Person对象设置几个xml命名的对象图,并从内部“引用”这些对象(或者更好地从内部)?我希望避免注释,只处理外部绑定文档。另外,我希望在外部绑定文档中尽可能多地进行配置,而在Java中则尽可能少地进行配置。@yglott-以下链接提供了在外部映射文档中配置命名对象图的示例:
    <?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>jdoe@example.com</email>
       </client>
    </policy>