Posts

Showing posts with the label JAXB

Customizing the JAXB marshalling/unmarshalling

The following example is used for customizing the way how the dates are being parsed, but it could be used for any other specific customizing import java . text . SimpleDateFormat ; import java . util . Date ; import javax . xml . bind . annotation . adapters . XmlAdapter ; public class DateAdapter extends XmlAdapter < String , Date > { private SimpleDateFormat dateFormat = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ); @Override public String marshal ( Date v ) throws Exception { return dateFormat . format ( v ); } @Override public Date unmarshal ( String v ) throws Exception { return dateFormat . parse ( v ); } } which then as adapter can be used to customize marshalling/unmarshalling... import java . util . Date ; import javax . xml . bind . annotation . XmlAccessorType ; import javax . xml . bind . annotation . XmlAccessType ; import javax . xml . bind . annot...

Chris Maki's Blog: Annotated JAXB Classes

Interesting, but JAXB sometimes is not able to use its own generated .java files. So, if you see the error javax.xml.bind.JAXBException: "your.package" doesnt contain ObjectFactory.class or jaxb.index the following post might be your solution: Chris Maki's Blog: Annotated JAXB Classes

Prevent JAXB from creating JAXBElements

I had some annoying trouble with updating JAXB1.0 code into JAXB2.0 code, among which the one of the things is the generation og JAXBElements instead normal type, like JAXBElements instead of simple double. This is due to fact that JAXB wants to define cases of elements in XSD with minOccurs="0" and nillable="true". If you don't care about such issue, then you can prevent JAXB from creating the JAXBElements in a following way: Add to your command line generation -b addCFG.xjb where addCFG.xjb is this file: xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"> source :