Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jaxb XmlAdapter将从同一xml中解组一个对象_Jaxb_Annotations - Fatal编程技术网

jaxb XmlAdapter将从同一xml中解组一个对象

jaxb XmlAdapter将从同一xml中解组一个对象,jaxb,annotations,Jaxb,Annotations,请看下面的代码: @XmlRootElement class Course { private int id; private String name; public Course() {} public Course(int id, String name) { super(); this.id = id; this.name = name; } @XmlAttribute(name = "id

请看下面的代码:

@XmlRootElement
class Course {
    private int id;
    private String name;

    public Course() {}
    public Course(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    @XmlAttribute(name = "id")
    public int getId() {
        return id;
    }
    @XmlAttribute(name = "name")
    public String getName() {
        return name;
    }
    public void setId(int id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }

}

class CourseAdapter extends XmlAdapter<Integer, Course>{

    @Override
    public Course unmarshal(Integer v) throws Exception {
        // what to do hereeeeeeeeeeeeeeee????!!!!
        // I want the Course with the id = v unmarshalled from
        // the same xml I am unmarshalling at the moment
    }

    @Override
    public Integer marshal(Course v) throws Exception {
        return v.getId();
    }

}

@XmlRootElement
class Offering {
    private int id;
    private Course course;
    private int capacity;

    public Offering() {}
    public Offering(int id, Course course, int capacity) {
        super();
        this.id = id;
        this.course = course;
        this.capacity = capacity;
    }

    @XmlAttribute(name = "id")
    public int getId() {
        return id;
    }
    @XmlJavaTypeAdapter(CourseAdapter.class)
    @XmlAttribute(name = "course")
    public Course getCourse() {
        return course;
    }
    @XmlAttribute(name = "capacity")
    public int getCapacity() {
        return capacity;
    }

    public void setId(int id) {
        this.id = id;
    }
    public void setCourse(Course course) {
        this.course = course;
    }
    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

}

@XmlRootElement
class Department {
    private String name;
    private ArrayList<Course> courses;
    private ArrayList<Offering> offerings;

    public Department(){}
    public Department(String name, ArrayList<Course> courses, ArrayList<Offering> offerings) {
        super();
        this.name = name;
        this.courses = courses;
        this.offerings = offerings;
    }
    @XmlAttribute(name = "name")
    public String getName() {
        return name;
    }
    @XmlElement
    public ArrayList<Course> getCourses() {
        return courses;
    }
    @XmlElement
    public ArrayList<Offering> getOfferings() {
        return offerings;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void setCourses(ArrayList<Course> courses) {
        this.courses = courses;
    }
    public void setOfferings(ArrayList<Offering> offerings) {
        this.offerings = offerings;
    }

}
@XmlRootElement
班级课程{
私有int-id;
私有字符串名称;
公共课程(){}
公共课程(整数id,字符串名称){
超级();
this.id=id;
this.name=名称;
}
@xmltattribute(name=“id”)
公共int getId(){
返回id;
}
@XmlAttribute(name=“name”)
公共字符串getName(){
返回名称;
}
公共无效集合id(内部id){
this.id=id;
}
公共void集合名(字符串名){
this.name=名称;
}
}
类CourseAdapter扩展了XmlAdapter{
@凌驾
公共课程解组(整数v)引发异常{
//在这里做什么????!!!!
//我希望id=v的课程从
//我现在正在解密的xml也是一样
}
@凌驾
公共整数封送处理(课程v)引发异常{
return v.getId();
}
}
@XmlRootElement
课堂教学{
私有int-id;
私人课程;
私人int能力;
公开发行(){}
公开发行(国际编号、课程、国际容量){
超级();
this.id=id;
本课程=课程;
这个。容量=容量;
}
@xmltattribute(name=“id”)
公共int getId(){
返回id;
}
@XmlJavaTypeAdapter(CourseAdapter.class)
@xmldattribute(name=“课程”)
公共课程{
返回路线;
}
@xmldattribute(name=“容量”)
公共int getCapacity(){
返回能力;
}
公共无效集合id(内部id){
this.id=id;
}
公共课程(课程){
本课程=课程;
}
公共容量(整数容量){
这个。容量=容量;
}
}
@XmlRootElement
班级部{
私有字符串名称;
私立ArrayList课程;
私人ArrayList产品;
公共部门(){}
公共部门(字符串名称、ArrayList课程、ArrayList产品){
超级();
this.name=名称;
本课程=课程;
这个。供品=供品;
}
@XmlAttribute(name=“name”)
公共字符串getName(){
返回名称;
}
@XmlElement
公共阵列列表getCourses(){
返回课程;
}
@XmlElement
公共阵列列表getOffices(){
回赠;
}
公共void集合名(字符串名){
this.name=名称;
}
公共课程(ArrayList课程){
本课程=课程;
}
公开发售(ArrayList发售){
这个。供品=供品;
}
}
我已经组织了我的部门,结果如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<department name="ece">
    <courses id="1" name="farsi"/>
    <courses id="2" name="dini"/>
    <courses id="2" name="riazi"/>
    <offerings capacity="10" course="1" id="1"/>
    <offerings capacity="20" course="2" id="2"/>
</department>


问题是,我不知道如何从这个通过CourseAdapter的“unmarshal”函数解组的xml中解组id=v的课程。

您可以使用
@XmlID
/
@XmlIDREF
来处理这个用例,而不需要
XmlAdapter


您可以将
@XmlID
/
@XmlIDREF
用于此用例,而无需
XmlAdapter