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

时间:2013-05-09 21:06:46

标签: 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")
    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;
    }

}

我已经整理了我的部门,结果就是这样:

<?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>

问题是我不知道如何通过这个非常xml解析使用id = v的过程,这个xml是通过CourseAdapter的“unmarshal”函数解组的。

1 个答案:

答案 0 :(得分:1)

您可以使用@XmlID / @XmlIDREF来完成此用例,而无需XmlAdapter