java - How to unmarshall XML to this class? -
i unmarshall xml using jaxb unmarshaller object of type.
public class pair<t1,t2> implements serializable { private t1 first; private t2 second; public pair(t1 first, t2 second) { this.first = first; this.second = second; } public t1 getfirst() { return first; } public void setfirst(t1 first) { this.first = first; } public t2 getsecond() { return second; } public void setsecond(t2 second) { this.second = second; } } public class trippair<t1,t2> extends pair<t1,t2> { public trippair(t1 first, t2 second) { super(first, second); } } public class fare extends pricing implements comparable<fare> { private list<trippair<integer,integer>> trips = new linkedlist<>(); }
xml file
<faregroups> <trips> <second>37</second> <first>0</first> </trips> </faregroups>
xml file have other data , unmarshalled, unfortunately data elements first
, second
not unmarshalled. tried add @xmlelement
annotations on these fields pair
class, without sucesss.
using xmladapter
public class pairadapter extends xmladapter<string, integer> { public string marshal(integer val) throws exception { return val.tostring(); } public integer unmarshal(string val) throws exception { return integer.valueof(val); } } public class pair<t1,t2> implements serializable { @xmljavatypeadapter(pairadapter.class) private t1 first; @xmljavatypeadapter(pairadapter.class) private t2 second; }
you need add zero-arg constructor, or create xmladapter
pair
class unmarshal correctly (see: http://blog.bdoughan.com/2010/12/jaxb-and-immutable-objects.html).
update
i'm using jaxb marshaller , when made debugging of non-marshalled element, has wrong type. elementnsimpl instead of integer.
as far jaxb concerned first
, second
treated type object
, since derives metadata @ class level pair
instenad of type level pair<integer, integer>
. because of xml need qualify xml elements necessary type information. done using xsi:type
attribute.
public class pair<t1,t2> implements serializable { private t1 first; private t2 second;
try populating object model data want , marshalling , see xml should unmarshal operation.
Comments
Post a Comment