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/を使用できます。@XmlIDREFXmlAdapter

于 2013-05-09T21:31:50.913 に答える