java - Unmarshall with JAXB doesn't work -



java - Unmarshall with JAXB doesn't work -

i have simpe xml want unmarshall model class. have annotated class jaxb annotations defining access type (field):

class="lang-java prettyprint-override">import javax.xml.bind.annotation.xmlaccesstype; import javax.xml.bind.annotation.xmlaccessortype; @xmlaccessortype(xmlaccesstype.field) public class dtotest { private string name; public dtotest() {} public dtotest(string name) { super(); this.name = name; } public string getname() { homecoming name; } public void setname(string name) { this.name = name; } @override public string tostring() { homecoming "dtotest [name=" + name + "]"; } }

this main class run unmarshal method against simple xml saved in string variable:

class="lang-java prettyprint-override">public class test { public static void main(string[] args) throws exception { object obj = new dtotest(); string testxml = "<dtotest><name>example</name></dtotest>"; obj = unmarshal(obj, testxml); system.out.println(obj); } /* generic unmarshall method i've used success other xml*/ public static <t> t unmarshal(t obj, string xml) throws exception { xmlinputfactory xif = xmlinputfactory.newfactory(); xmlstreamreader xsr = xif.createxmlstreamreader(new stringreader(xml)); class<? extends object> type = obj.getclass(); jaxbcontext jc = jaxbcontext.newinstance(type); unmarshaller unmarshaller = jc.createunmarshaller(); obj = (t)unmarshaller.unmarshal(xsr, type).getvalue(); xsr.close(); homecoming obj; } }

whenever run code same output:

class="lang-java prettyprint-override">dtotest [name=null]

i don't understand i'm doing wrong.

i've run code on jdk1.7.0_67 , works.

dtotest [name=example]

maybe have problem included libraries? i've run plain java.

java xml jaxb

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -