【发布时间】:2017-01-23 14:13:30
【问题描述】:
我对@987654321@ 不是很熟悉,但我在一个项目中试了一下,XML 生成得很好。但是当我应用XML validation (with the .xsd file) 时,它会显示一个错误,说<Timestamp> 是空的。我调试了应用程序,我看到在我的 Bean 中 Timestamp 是 not 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