繁体   English   中英

JAXB将引用列表绑定到另一个元素

[英]JAXB binding the list of references to another element

用一个例子更容易解释,

样本XML:

<root>  
 <company>
        <name>xyz</name>
        <employees>
           <employeeref>emp1</employeeref>
           <employeeref>emp2</employeeref>
        </employees>   
 </company>

      <employee id="emp1">
         <name>a</name>
         <age>12</age>   </employee>

      <employee id="emp2">
         <name>b</name>
         <age>24</age>   </employee>

      <employee id="emp3">
         <name>c</name>
         <age>36</age>   
      </employee> 
</root>

这将需要Company.java和Employee.java。

我的问题是,如何使用Jaxb批注将xml绑定到java对象?

您可以将@XmlID@XmlIDREF结合使用

雇员

Employee类上,您需要注释哪个字段/属性将成为对象的“关键”。 在JAXB中,这是通过@XmlID批注完成的。

@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {

    @XmlAttribute
    @XmlID
    private String id;
}

公司

Company类中,我们将指示我们想要封送Employee实例作为对真实对象的引用。 这是使用@XmlIDREF批注完成的。

@XmlAccesorType(XmlAccessType.FIELD)
public class Company {

    @XmlElementWrapper
    @XmlElement(name="elementref")
    @XmlIDREF
    private List<Employee> employees;
}

为了使@XmlIDREF起作用,该对象必须使用@XmlElement批注映射到其他位置。 在此示例中,这在Root类中很重要。

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    private Company company;

    @XmlElement(name="employee")
    private List<Employee> employees;

}

欲获得更多信息

您可以在我的博客上阅读有关@XmlID@XmlIDREF更多信息:

我认为您需要上课,这里是示例,您可以通过其他方式进行

Root    One class for Root
    | -- Company  Company class
    |  |
    |  | employess(List)  one more class for employess collection
    |         
    |         
    employee (collection) one class for employee

员工资料

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "root")
public class EmployeeData {
    @XmlElement
    private Company company;
    private List<Employee> employee; 
// getter/setter
}

公司

@XmlAccessorType(XmlAccessType.FIELD)
public class Company {
    @XmlElement(required = true)
    private String name;
    @XmlElement(required = true)
    private Employees employees;
//...// getter/setter
}

雇员

@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
        @XmlElement
    private String name;
    private int age;
    @XmlAttribute(name = "id")
    private String id;
///// getter/setter
}

雇员

@XmlAccessorType(XmlAccessType.FIELD)
public class Employees {
        private List<String> employeeref;

// getter / setter}

Java类的结构可以使其形成XML文件中描述的树层次结构。 Java类中使用注释来描述此结构。 这是一些使用的注释的列表:

  • @XmlRootElement:将类或枚举类型映射到XML元素。
  • @XmlAttribute:将JavaBean属性映射到XML属性。
  • @XmlElement:将JavaBean属性映射到从属性名称派生的XML元素

Employee类可以像这样:

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Employee {
    private String id;

    private String name;

    private String age;

    public String getId() {
        return id;
    }
    @XmlAttribute
    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    @XmlElement
    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }
    @XmlElement
    public void setAge(String age) {
        this.age = age;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM