jaxb引用xml文件之间的xmlID

时间:2015-10-01 21:56:27

标签: java xml jaxb

我在this post

中尝试了这种方法

但是我得到了

>

<?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文件如下:

employee.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>

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

employee.xml引用了部门,我想在解组employee.xml时指向正确的部门对象。

课程如下:

Company.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;
     }

Employee.java

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

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

Department.java

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

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

DepartmentList.java

 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));

然后我在Main

中运行以下内容
$direcotry

我觉得有一个XMLIDREF引用员工ID而另一个XMLIDREF引用部门ID是问题的一部分。但这是必需的,因为manager字段引用了其他员工对象。

有人可以帮我解决这个问题。谢谢

1 个答案:

答案 0 :(得分:0)

问题出现在类公司,它对应于包含员工部门的XML文档。但是,您有两个单独的文档。显然你想要一个包含两个列表的最终类。

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

(2)更改Company.departments的注释

@XmlTransient
private List<Department> departments;

marshal就像你现在一样,并使用你的引用设置List,将相应的XML解组为返回的对象。