annotations - jaxb XmlAdapter unmarshall an object from the same xml -
take @ code :
@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 { // hereeeeeeeeeeeeeeee????!!!! // want course id = v unmarshalled // same xml unmarshalling @ 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; } }
i have marshalled department , result:
<?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>
the problem not know how unmarshal course id = v xml being unmarshalled via "unmarshal" function of courseadapter .
you can use @xmlid
/@xmlidref
use case without requiring xmladapter
.
Comments
Post a Comment