有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java jaxb在xml文件之间引用xmlID

我在this post中尝试了这种方法

但是我得到了一个

>

 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>

部门。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>

雇员。xml引用部门,我想在解组员工时指向正确的部门对象。xml

课程如下:

公司。爪哇

 @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>();
    }
    ...
}

雇员。爪哇

 @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;
     }

部门。爪哇

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

@XmlAttribute
@XmlID
private String id;
...
}

部门名单。爪哇

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

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

然后我在Main中运行以下命令

 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指的是员工id,另一个XMLIDREF指的是部门id是问题的一部分。但这是必需的,因为manager字段引用了其他雇员对象

谁能帮我一下吗。谢谢!


共 (1) 个答案

  1. # 1 楼答案

    问题源于与包含员工和部门的XML文档相对应的类Company。但是,您有两个单独的文档。显然你想要最后一个包含两个列表的类

    (1)您可以仅为员工定义类EmployeeList,类似于为部门(DepartmentList)定义类EmployeeList。这仍然允许您编写一个应用程序类公司,在其中设置两个列表的引用

    (2)更改公司的注释。部门

    @XmlTransient
    private List<Department> departments;
    

    像现在一样封送,并使用从将相应的XML解组到返回对象中得到的引用设置列表