2013-05-09 81 views
1

看看这个代码:JAXB XmlAdapter和解组对象来自同一个XML

@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的“unmarshal”函数解组的unmarshalled id = v解组课程。

回答