简体   繁体   English

JAXB乱序解组引用@XmlID和@XmlIDREF

[英]JAXB Out of order unmarshalling references @XmlID and @XmlIDREF

I have the following xml: 我有以下xml:

Company.xml Company.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<company>
    <employeeList>
        <employee name="Jane Doe" id="A">
        </employee>
        <employee name="John Smith" id="B">
        </employee>
        <employee name="Anne Jones" id="C">
        </employee>
    </employeeList>
</company>

Department.xml Department.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<departmentList>
    <departmentList>
        <department name="Dev" id="1">
            <employee>A</employee>
            <employee>B</employee>
        </department>
        <department name="Sales" id="2">
            <employee>C</employee>
        </department>
    </departmentList>
</departmentList>

The class Department.java has an employee list Department.java类具有员工列表

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

    @XmlAttribute
    private String id;

    @XmlElement(name="employee")
    @XmlJavaTypeAdapter(EmpAdapter.class)
    public List<Employee> employeeList; 

    public Department(){
        employeeList = new ArrayList<Employee>();
    }

and the employee.java class has an id attribute with the @XmlId tag. 并且employee.java类具有带有@XmlId标记的id属性。

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

        @XmlAttribute
        @XmlID
        private String id;

I know how to get the employee objects to be pointed at from the department object using an adapter. 我知道如何使用适配器从部门对象中获取要指向的雇员对象。 However that is if the Company.xml file is unmarshalled first because it has the employee details in it. 但是,如果Company.xml文件因为其中包含员工详细信息而首先被取消编组,那就是这样。

So what I want is to be able to unmarshall the Department.xml first and have it create some sort of placeholder so that once the Company.xml has been unmarshalled the employeeList in the department object is filled in. 因此,我想要的是能够首先解组Department.xml并创建某种占位符,以便一旦Company.xml解组后,部门对象中的employeeList就会被填充。

EDIT 编辑

POSSIBLE SOLUTION : As suggested by @laune I can use the adapter to create Employee objects as a placeholder, and then once the employee list is unmarshalled fill in these details in the employee objects under the department class 可能的解决方案 :如@laune所建议,我可以使用适配器将Employee对象创建为占位符,然后在解组员工列表后,将这些详细信息填写在Department类下的employee对象中

WHAT I NEED: But this way I have to always handle each of the cases individually through code and new methods. 我需要:但是这样,我必须始终通过代码和新方法分别处理每种情况。 Main problem is I cannot predict the order in which the unmarshalling will happen. 主要问题是我无法预测解组的顺序。 Is there a way to have JAXB unmarshall without worrying about dependencies and ordering of creating objects? 有没有一种方法可以使JAXB解组而不用担心依赖关系和创建对象的顺序? Maybe using schemas? 也许使用架构?

You can modify the EmpAdapter to return a placeholder Employee object created from an (additional) constructor 您可以修改EmpAdapter以返回从(其他)构造函数创建的占位符Employee对象。

public Employee( String id ){
    this.id = id;
}

Also, the EmpAdapter should maintain a Map<String,Employee> mapping ids to employees. 另外,EmpAdapter应该维护一个Map<String,Employee>映射ID到雇员。

After you have unmarshalled the employees into a flat list, iterate the list and fill the fields of the placeholder (located via the map) from the list element. 将员工解组到一个平面列表后,请迭代该列表,并从list元素填充占位符(通过地图定位)的字段。

Things would be much easier if employee data contains the deparment id. 如果员工数据包含部门编号,事情会容易得多。

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

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