Jaxb子属性具有相同的名称映射到父对象

时间:2014-06-23 15:09:33

标签: java xml jaxb

我有一个这种格式的xml文档:

<company>
    <employees>
        <employee firstname="xyz" lastname="abc">
            <empId ID="1"/>
            <department ID="D1"/>
            <manager ID="23"/>
        </employee>
    </employees>
    <departments>
        <department name="dept 1">
            <deptId ID="D1"/>
        </department>
    </departments>
 <company>

我正在尝试使用JAXB将其解组为Java,但无法确定将员工ID映射到Employee类中的id字段的方法。

public class Employee{

@XmlAttribute
private String firstname;

@XmlAttribute
private String lastname;

???
@XmlID
private String id;

???
@XmlIDREF
private String managerId;

}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

根据一些评论反馈,您可以尝试使用以下内容编组数据:

创建两个类来表示对象类中的ID字段:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="deptId")
public class DepartmentId {
    @XmlAttribute(name="ID") // define an attribute for the ID
    @XmlID
    String id;
    @XmlValue  // define a value, and set to null
    String value = null;

    public DepartmentId() {}

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

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="empId")
public class EmployeeId {
    @XmlAttribute(name="ID")
    @XmlID
    String id;
    @XmlValue
    String value = null;

    public EmployeeId() {}

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

注释您的Employee类,如下所示: 你的getter / setter方法可能需要一些微小的mod,这很粗糙,但它应该适用于下面的测试,as-is

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="employee")
public class Employee {

    @XmlElement(name="empId")
    private EmployeeId id; // an employee ID object will be used to represent this element

    @XmlAttribute(name="firstname")
    private String firstName;

    @XmlAttribute(name="lastname")
    private String lastName;

    @XmlElement(name="manager")
    private EmployeeId manager;  // same with the manager

    @XmlElement(name="department")
    private DepartmentId department;  // same with the dept

    public Employee() {

    }

    public void setId(String id) {
        this.id = new EmployeeId();
        this.id.setId(id);
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setManager(Employee manager) {
        this.manager = new EmployeeId();
        this.manager.setId(manager.getId());
    }

    public void setDepartment(Department department) {
        this.department = new DepartmentId();
        this.department.setId(department.getId());
    }

    public String getId() { return this.id.getId(); }
    public String getFirstName() { return this.firstName; }
    public String getLastName() { return this.lastName; }
    public EmployeeId getManager() { return this.manager; }
    public DepartmentId getDepartment() { return this.department; }
}

Department类注释如下:

@XmlRootElement(name="department")
@XmlAccessorType(XmlAccessType.FIELD)
public class Department {

    @XmlElement(name="deptId")
    private DepartmentId id;

    public Department() {}

    public void setId(String id) {
        this.id = new DepartmentId();
        this.id.setId(id);
    }
    public String getId() { return this.id.getId(); }
}

Company类注释如下:

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

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

    @XmlElement(name="department")
    private List<Department> departments;

    public Company() {
        employees = new ArrayList<Employee>();
        departments = new ArrayList<Department>();
    }

    public List<Employee> getEmployees() { return this.employees; }
    public void setEmployees(List<Employee> employees) { this.employees = employees; }
    public List<Department> getDepartments() { return this.departments; }
    public void setDepartments(List<Department> departments ) { this.departments = departments; }
}

通过以下测试:

public class Test {
    public static void main(String[] args) throws Exception {
        Company c = new Company();
        Department d1 = new Department();
        d1.setId("D1");
        Department d2 = new Department();
        d2.setId("D2");

        Employee e1 = new Employee();
        e1.setId("1");
        e1.setFirstName("Joe");
        e1.setLastName("Smith");
        e1.setDepartment(d1);

        Employee e2 = new Employee();
        e2.setId("2");
        e2.setFirstName("John");
        e2.setLastName("Doe");
        e2.setManager(e1);
        e2.setDepartment(d1);

        c.getEmployees().add(e1);
        c.getEmployees().add(e2);
        c.getDepartments().add(d1);
        c.getDepartments().add(d2);

        JAXBContext context = JAXBContext.newInstance(Company.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(c, System.out);
    }
}

生成以下XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<company>
    <employee firstname="Joe" lastname="Smith">
        <empId ID="1"/>
        <department ID="D1"/>
    </employee>
    <employee firstname="John" lastname="Doe">
        <empId ID="2"/>
        <manager ID="1"/>
        <department ID="D1"/>
    </employee>
    <department>
        <deptId ID="D1"/>
    </department>
    <department>
        <deptId ID="D2"/>
    </department>
</company>

由于您有嵌入式标记employeesdepartments,因此您需要使用另外几个类来处理嵌套级别。看看这是否适合你。

有关其他信息,请参阅this referencethis reference