3

I want to convert an Object with a java.time.Localdate (JSR-310) and a property wrapping a Localdate to XML via JAXB but the output is wrong.

public <T> void printPdf(T obj) {
    // create xml
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    StringWriter outWriter = new StringWriter();
    StreamResult result = new StreamResult(outWriter);
    m.marshal(obj, result);

    StringBuffer sb = outWriter.getBuffer();
    String finalstring = sb.toString();
    System.out.println(finalstring);
}

This is the Object:

@XmlRootElement
public class Invoice implements Validateable {
    private LocalDate date = LocalDate.now();
    private ObjectProperty<LocalDate> dueDate = new SimpleObjectProperty<LocalDate>(
            LocalDate.now().plusDays(20));
    // ========================================================================
    // date
    // ========================================================================
    public LocalDate getDate() {
        return date;
    }
    public void setDate(LocalDate date) {
        this.date = date;
    }
    // ========================================================================
    // companyName
    // ========================================================================
    public LocalDate getDueDate() {
        return this.dueDate.get();
    }
    public void setDueDate(LocalDate duedate) {
        this.dueDate.set(duedate);
    }
    public final ObjectProperty<LocalDate> dueDateProperty() {
        return dueDate;
    }
}

This is the XML output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<invoice>
    <date/>
    <dueDate/>
</invoice>

How can I convert this properly? I'm not stuck to use JAXB if there is something better out there.

JodaStephen
  • 60,927
  • 15
  • 95
  • 117
simonides
  • 3,040
  • 3
  • 19
  • 34
  • same subject here: http://stackoverflow.com/questions/24251183/can-jaxb-handle-java-time-objects – pdem Mar 31 '16 at 16:06

1 Answers1

1

You can use a JAXB XmlAdapter to control how the JSR-310 objects are converted to/from XML.

bdoughan
  • 147,609
  • 23
  • 300
  • 400