简体   繁体   中英

XStream unmarshalling XMLGregorianCalendar

I have some field, where the xsd has it as an xs:dateTime

For some reason, when it was marshalled, it was stored as below.

    <dateIssuedField class="org.apache.xerces.jaxp.datatype.XMLGregorianCalendarImpl" resolves-to="org.apache.xerces.jaxp.datatype.SerializedXMLGregorianCalendar">
      <lexicalValue>2021-08-06T00:00:00</lexicalValue>
    </dateIssuedField>

I'm getting this error below.

XStream unmarshalling failed.
com.thoughtworks.xstream.converters.ConversionException: Cannot construct type
---- Debugging information ----
message             : Cannot construct type
cause-exception     : java.lang.InstantiationException
cause-message       : javax.xml.datatype.XMLGregorianCalendar
construction-type   : javax.xml.datatype.XMLGregorianCalendar
class               : javax.xml.datatype.XMLGregorianCalendar
required-type       : javax.xml.datatype.XMLGregorianCalendar
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter

Any idea how to write a converter to handle this?

Ok worked it out.

        xstream.registerConverter(new Converter() {
            @Override
            public boolean canConvert(Class arg0) {
                return arg0.equals(javax.xml.datatype.XMLGregorianCalendar.class);
            }

            @Override
            public Object unmarshal(HierarchicalStreamReader arg0, UnmarshallingContext arg1) {
                return null;
            }

            @Override
            public void marshal(Object arg0, HierarchicalStreamWriter arg1, MarshallingContext arg2) {
                XMLGregorianCalendar x = (XMLGregorianCalendar) arg0;
                arg1.setValue(x.toString());
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM