Skip to content
Advertisement

Marshalling LocalDate using JAXB

I’m building a series of linked classes whose instances I want to be able to marshall to XML so I can save them to a file and read them in again later.

At present I’m using the following code as a test case:

JavaScript

The XML output is:

JavaScript

Is there a reason why the startDate element is empty? I would like it to contain the string representation of the date (i.e. toString()). Do I need to write some code of my own in order to do this?

The output of java -version is:

JavaScript

Advertisement

Answer

You will have to create an XmlAdapter like this:

JavaScript

And annotate your field using

JavaScript

See also javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters if you want to define your adapters on a package level.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement