XML中的时间戳标记为空 - Java Marshaller

时间:2017-01-23 14:13:30

标签: java xml timestamp marshalling

我对Marshaller不是很熟悉,但我尝试了一个项目,XML生成正常。但是当我应用XML validation (with the .xsd file)时,它会显示一条错误,指出<Timestamp>为空。我调试了应用程序,我发现在我的Bean中Timestampnot empty但是当Marshaller生成XML时它真的是空的。另一方面,XML中存在使用XMLGregorianCalendar的其他属性。我不知道发生了什么。

这是XML生成器函数:

public void generateXMLReportFile(String fileOutputDirectory,
                                  String xmlOutputFileName,
                                  CRSOECD crsOECD)
    throws JAXBException, FileNotFoundException, IOException
{
    String encoding = "UTF-8";
    // Schema location to write to generated XML file.
    String schemaLocation = "urn:oecd:ties:crs:v1 CrsXML_v1.0.xsd";

    // Generate The Report:
    JAXBContext context = JAXBContext.newInstance(CRSOECD.class);

    StringWriter xml = new StringWriter();

    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
    marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation);

    marshaller.marshal(crsOECD, xml);

    try (OutputStream out = new FileOutputStream(fileOutputDirectory + xmlOutputFileName)) {
        byte[] bytes = xml.toString().getBytes(encoding);
        out.write(bytes, 0, bytes.length);
        log.info("XML generated.");
    }
}

如果我调试,我发现在Bean crsOECD中,XML中为空的元素不为空。

1 个答案:

答案 0 :(得分:1)

伙计我刚刚找到答案。问题是我的元素被标记为:

@XmlElement(name = "Timestamp", required = true) @XmlSchemaType(name = "dateTime")

我只传递Date而没有Time