java - Strange JaxB-Behavior when (un)marshaling XMLGregorianCalender -
the main class:
public class checkjaxbbehavior { private string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><date day=\"---10+02:00\" month=\"--09--+02:00\" year=\"2014+02:00\"/>"; public datetype unmarshal() throws exception { bytearrayinputstream input = new bytearrayinputstream(xml.getbytes()); jaxbcontext jaxbcontext = jaxbcontext.newinstance(datetype.class); unmarshaller jaxbunmarshaller = jaxbcontext.createunmarshaller(); datetype date = (datetype) jaxbunmarshaller.unmarshal(input); return date; } private static xmlgregoriancalendar createxmlgregoriandate(date date) throws exception { gregoriancalendar gregoriancalendar = new gregoriancalendar(); gregoriancalendar.settime(date); xmlgregoriancalendar xmlgregoriancalendar = datatypefactory.newinstance().newxmlgregoriancalendar(gregoriancalendar); return xmlgregoriancalendar; } private void marshalandprint(datetype datetype) throws exception { jaxbcontext jaxbcontext = jaxbcontext.newinstance(datetype.class); marshaller jaxbmarshaller = jaxbcontext.createmarshaller(); jaxbmarshaller.setproperty(marshaller.jaxb_formatted_output, true); stringwriter stringwriter = new stringwriter(); jaxbmarshaller.marshal(datetype, stringwriter); system.out.println(stringwriter); } public static void main(string[] args) throws exception { checkjaxbbehavior checkit = new checkjaxbbehavior(); datetype datetype = checkit.unmarshal(); system.out.println("day: " + datetype.getday()); system.out.println("month: " + datetype.getmonth()); system.out.println("year: " + datetype.getyear()); xmlgregoriancalendar xmlgregoriancalendar = createxmlgregoriandate(new date()); system.out.println(xmlgregoriancalendar.toxmlformat()); datetype datetypetomarshal = new datetype(); datetypetomarshal.setday(xmlgregoriancalendar); datetypetomarshal.setmonth(xmlgregoriancalendar); datetypetomarshal.setyear(xmlgregoriancalendar); checkit.marshalandprint(datetypetomarshal); } }
the data class:
@xmlrootelement(name="date") @xmlaccessortype(xmlaccesstype.field) public class datetype { @xmlattribute(name = "day") @xmlschematype(name = "gday") protected xmlgregoriancalendar day; @xmlattribute(name = "month") @xmlschematype(name = "gmonth") // @xmljavatypeadapter(value=gmonthadapter.class) protected xmlgregoriancalendar month; @xmlattribute(name = "year", required = true) @xmlschematype(name = "gyear") protected xmlgregoriancalendar year; // getters , setters }
the output:
day: ---10+02:00 month: null year: 2014+02:00 2014-09-10t17:05:01.768+02:00 <?xml version="1.0" encoding="utf-8" standalone="yes"?> <date day="---10+02:00" month="--09--+02:00" year="2014+02:00"/>
my question: i'm using jkd 1.7.67 , little bit strange! input string same output string, jaxb isn't able unmarshal month. think output not correct, --09+2:00 must correct value. unknown bug or wrong code? wrote adapter fix possibly there solution?
the lexical representation type xs:gmonth --mm, optional time zone following.
if use
"... month=\"--09+02:00\" ... "
in input string xml, month won*t null more:
day: ---10+02:00 month: --09+02:00 year: 2014+02:00
but marshalling exhibits error in jaxb, since @month has incorrect value.
Comments
Post a Comment