1

看看这段代码:

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

问题是我不知道如何从这个通过 CourseAdapter 的“解组”功能解组的 xml 中使用 id = v 解组课程。

4

1 回答 1

1

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

于 2013-05-09T21:31:50.913 回答