Я попробовал подход в this postссылка JAXB xmlID между XML-файлов
Однако я получаю
>
1 counts of IllegalAnnotationExceptions
XmlIDREF property is referencing a type "java.lang.String" that doesn't have an XmlID property.
this problem is related to the following location:
at private externalReferences.Department
externalReferences.Employee.department
at externalReferences.Employee
at private java.util.List externalReferences.Company.employees
at externalReferences.Company
Эти два файла XML являются следующие:
работника. xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<company>
<employeeList>
<employee name="Jane Doe" id="1">
<department>1</department>
</employee>
<employee name="John Smith" id="2">
<department>2</department>
</employee>
<employee name="Anne Jones" id="3">
<department>3</department>
</employee>
</employeeList>
</company>
department.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<departmentList>
<departmentList>
<department name="Dev" id="1"/>
<department name="Sales" id="2"/>
<department name="Research" id="3"/>
</departmentList>
</departmentList>
employee.xml ссылается на отдел, и я хочу, чтобы указать на нужный отдел объектов при демаршаллизации employee.xml.
Классы являются следующие:
Company.java
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Company {
@XmlElementWrapper(name = "employeeList")
@XmlElement(name="employee")
private List<Employee> employees;
@XmlElementWrapper(name = "departmentList")
@XmlElement(name="department")
private List<Department> departments;
public Company() {
employees = new ArrayList<Employee>();
departments = new ArrayList<Department>();
}
...
}
Employee.java
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
@XmlAttribute
@XmlID
private String id;
public String getId() {
return id;
}
@XmlIDREF
private Employee manager;
@XmlJavaTypeAdapter(EmpAdapter.class)
@XmlIDREF
private Department department;
}
Department.java
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Department {
@XmlAttribute
@XmlID
private String id;
...
}
DepartmentList.java
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class DepartmentList {
@XmlElementWrapper(name = "departmentList")
@XmlElement(name="department")
private List<Department> departments;
Затем я запускаю следующее в главном
JAXBContext jc = JAXBContext.newInstance(DepartmentList.class); Unmarshaller unmarshaller = jc.createUnmarshaller();
DepartmentList depList = (DepartmentList) unmarshaller.unmarshal(new FileReader(DepRef));
EmpAdapter adapter = new EmpAdapter();
for(Department dep : depList.getDepartments()) {
adapter.getDepList().put(dep.getId(), dep);
}
JAXBContext jc2 = JAXBContext.newInstance(Company.class);
Unmarshaller unmarshaller2 = jc2.createUnmarshaller();
unmarshaller2.setAdapter(adapter);
Company company2 = (Company) unmarshaller2.unmarshal(new FileReader(empRef));
Я чувствую, что имея один XMLIDREF относятся к идентификатору сотрудника, а другой XMLIDREF см отдела ид является частью проблемы. Но это необходимо, так как поле менеджера ссылается на другие объекты-сотрудники.
Может кто-то, пожалуйста, помогите мне в этом. Спасибо