我有一个运行良好的xml文件编写器类,它使用jaxb2marshaller编写最终的xml文件,代码如下:
final String XSD_FILE_NAME = myxsd.xsd
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setSchema(resourceLoader.getResource("classpath:xml/" + XSD_FILE_NAME));
marshaller.setContextPath("com.my.project.type");
marshaller.setMarshallerProperties(ImmutableMap.of(
JAXB_FORMATTED_OUTPUT, Boolean.TRUE,
JAXB_NO_NAMESPACE_SCHEMA_LOCATION, XSD_FILE_NAME
));
marshaller.afterPropertiesSet();
// this is the way to write into cache for validation:
JAXBElement myFileToMarshall = new ObjectFactory().createMyFile();
try {
marshaller.marshal(myFileToMarshall, new SAXResult(new DefaultHandler()));
} catch (XmlMappingException xme) {
throw new RuntimeException("Unable to create exact xml file, because the data is not valid!", xme);
}
try (FileOutputStream fileOutputStream = new FileOutputStream("C:/tmp/file.xml")) {
marshaller.marshal(myFileToMarshall, new StreamResult(fileOutputStream));
} catch (IOException e) {
throw new RuntimeException("Exact XML writing failed", e);
}
xsd的开头如下:
targetNamespace="http://xmlns.myproject.com/v1"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://xmlns.myproject.com/v1">
最终生成的文件的开头如下所示:
我想要的是:
我想从最终文件中删除
xmlns="http://xmlns.myproject.com/v1"
部分,但能够使用xsd验证最终文件。
有可能吗?