简体   繁体   English

我想通过JAXB将带有java.time.Localdate(JSR-310)的Object转换为XML,但输出错误

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

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. 我想转换一个带有java.time.Localdate(JSR-310)的Object和一个通过JAXB将Localdate包装到XML但是输出错误的属性。

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输出:

<?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. 如果有更好的东西,我不会停止使用JAXB。

You can use a JAXB XmlAdapter to control how the JSR-310 objects are converted to/from XML. 您可以使用JAXB XmlAdapter来控制JSR-310对象如何转换为XML或从XML转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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