java - Jaxb - map attribute of list element [SOLVED] -
hy, xml looks this:
<forecast> <time day="2014-06-02"> <symbol></symbol> </time> <time day="2014-06-03"> <symbol></symbol> </time> </forecast>
i need map day attribute each "time" object looks didn't work expect.
heres classes (java):
public class forecast { @xmlelement public list<weatherevent> time; } public class weatherevent { @xmlattribute(name = "day") @xmljavatypeadapter(dateadapter.class) public date day; @xmlelement public symbol symbol; } public class dateadapter extends xmladapter<string, date> { private final simpledateformat dateformat = new simpledateformat( "yyyy-mm-dd't'hh:mm:ss"); @override public string marshal(date v) throws exception { return dateformat.format(v); } @override public date unmarshal(string v) throws exception { date date = dateformat.parse(v); if (date == null) { simpledateformat simplierformat = new simpledateformat("yyyy-mm-dd"); date = simplierformat.parse(v); } return date; } }
how map "day" attribute make not null?
the following line going throw parseexception
, exit method , never logic below when date looks like: 2014-06-02
.
date date = dateformat.parse(v);
you need catch exception , ignore it, , apply second formatter it.
date date = null; try { date date = dateformat.parse(v); } catch(parseexception e) { simpledateformat simplierformat = new simpledateformat("yyyy-mm-dd"); date = simplierformat.parse(v); } return date;
Comments
Post a Comment