java - Unmarshalling an element with attributes using JAXB -
java - Unmarshalling an element with attributes using JAXB -
i'm able marshall object xml, , vice versa except 2 elements attributes. hits i'm missing. null both attributes (role)
the unmarschel code
jaxbcontext context = jaxbcontext .newinstance(userlistwrapper.class); unmarshaller um = context.createunmarshaller(); // reading xml file , unmarshalling. userlistwrapper wrapper = (userlistwrapper) um.unmarshal(file); userdata.clear(); userdata.addall(wrapper.getusers());
this xml file
<users> <user action="insert" id="test.user" language="de"> <birthday>2000-01-01</birthday> <city>ads</city> <firstname>test</firstname> <gender>f</gender> <role action="assign" id="wqeqw" type="global">wqeqw</role> <lastname>user</lastname> <role action="assign" id="qweqwe" type="local">qweqwe</role> <login>sfrohwein</login> <matriculation>ads</matriculation> <postalcode>0</postalcode> <street>asd</street> </user><users>
the class role
@xmlrootelement(name="role") public class role { private string id; private string type; private string action; public role() { this(null,null); } public role(string type) { settype(type); } public role(string id, string type) { setid(id); settype(type); setaction("assign"); } /** * @return id */ @xmlattribute(name="id") public string getid() { homecoming id; } /** * @param id id set */ public void setid(string id) { this.id = id; } /** * @return type */ @xmlattribute(name="type") public string gettype() { homecoming type; } /** * @param type type set */ public void settype(string type) { this.type = type; } /** * @return action */ @xmlattribute(name="action") public string getaction() { homecoming action; } /** * @param action action set */ public void setaction(string action) { this.action = action; } /** * @return value */ @xmlvalue public string getvalue() { homecoming this.id; }
the class user
public class user { private final role globalrole; private final role localrole; private final stringproperty login; private final stringproperty firstname; private final stringproperty lastname; private final stringproperty matriculation; private final stringproperty gender; private final stringproperty street; private final integerproperty postalcode; private final stringproperty city; private final objectproperty<localdate> birthday; /** * default constructor. */ public user() { this(null, null); } /** * constructor initial data. * * @param firstname * @param lastname */ public user(string firstname, string lastname) { this.firstname = new simplestringproperty(firstname); this.lastname = new simplestringproperty(lastname); // initial dummy data, convenient testing. this.globalrole = new role("global"); this.localrole = new role("local"); this.gender = new simplestringproperty("f"); this.login = new simplestringproperty(""); this.matriculation = new simplestringproperty(""); this.street = new simplestringproperty(""); this.postalcode = new simpleintegerproperty(); this.city = new simplestringproperty(""); this.birthday = new simpleobjectproperty<localdate>(localdate.of(2000, 1, 1)); } @xmlelement(name = "login") public string getlogin() { homecoming login.get(); } public void setlogin(string login) { this.login.set(login); } public stringproperty loginproperty() { homecoming login; } @xmlelement(name = "matriculation") public string getmatriculation() { homecoming matriculation.get(); } public void setmatriculation(string matriculation) { this.matriculation.set(matriculation); } public stringproperty matriculationproperty() { homecoming matriculation; } @xmlelement(name = "firstname") public string getfirstname() { homecoming firstname.get(); } public void setfirstname(string firstname) { this.firstname.set(firstname); } public stringproperty firstnameproperty() { homecoming firstname; } @xmlelement(name = "lastname") public string getlastname() { homecoming lastname.get(); } public void setlastname(string lastname) { this.lastname.set(lastname); } public stringproperty lastnameproperty() { homecoming lastname; } @xmlelement(name = "street") public string getstreet() { homecoming street.get(); } public void setstreet(string street) { this.street.set(street); } public stringproperty streetproperty() { homecoming street; } @xmlelement(name = "postalcode") public int getpostalcode() { homecoming postalcode.get(); } public void setpostalcode(int postalcode) { this.postalcode.set(postalcode); } public integerproperty postalcodeproperty() { homecoming postalcode; } @xmlelement(name = "city") public string getcity() { homecoming city.get(); } public void setcity(string city) { this.city.set(city); } public stringproperty cityproperty() { homecoming city; } @xmlelement(name = "birthday") @xmljavatypeadapter(localdateadapter.class) public localdate getbirthday() { homecoming birthday.get(); } public void setbirthday(localdate birthday) { this.birthday.set(birthday); } public objectproperty<localdate> birthdayproperty() { homecoming birthday; } @xmlelement(name = "gender") public string getgender() { homecoming gender.get(); } public void setgender(string gender) { this.gender.set(gender); } public stringproperty genderproperty() { homecoming gender; } @xmlelement(name = "role") public role getglobalrole() { homecoming globalrole; } public void setglobalrole(string globalrole) { this.globalrole.setid(globalrole); } @xmlelement(name = "role") public role getlocalrole() { homecoming localrole; } public void setlocalrole(string localrole) { this.localrole.setid(localrole); }
class userlistwrapper
@xmlrootelement(name = "users") public class userlistwrapper { private list<user> users; @xmlelement(name = "user") public list<user> getusers() { homecoming users; } public void setusers(list<user> users) { this.users = users; } }
your role setter methods not true setter methods, , role fields marked final
, preventing giving class true setter method field.
i suggest rid of final modifier, , give user class true setter methods role fields.
java xml jaxb
Comments
Post a Comment