【问题标题】:Timestamp tag empty in XML - Java MarshallerXML 中的时间戳标记为空 - Java Marshaller
【发布时间】:2017-01-23 14:13:30
【问题描述】:

我对@9​​87654321@ 不是很熟悉,但我在一个项目中试了一下,XML 生成得很好。但是当我应用XML validation (with the .xsd file) 时,它会显示一个错误,说<Timestamp> 是空的。我调试了应用程序,我看到在我的 Bean 中 Timestampnot empty 但是当 Marshaller 生成 XML 时它真的是空的。另一方面,使用XMLGregorianCalendar 的其他属性存在于XML 中。我不知道发生了什么。

这是 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.");
    }
}

如果我调试,我看到 XML 中为空的元素在 Bean crsOECD 中不是空的。

【问题讨论】:

  • crsOECD.timestamp 在 Java 类中的实际类型是什么?也许您需要在 bindings.xjb 文件中定义的类型转换器。

标签: java xml timestamp marshalling


【解决方案1】:

伙计们,我刚刚找到了答案。问题是我的元素被这样标记:

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

我只传递了一个Date,没有传递Time

【讨论】:

    猜你喜欢
    • 2010-12-13
    • 2016-05-16
    • 1970-01-01
    • 2016-04-26
    • 2022-11-01
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多