I have a perfectly working XML file writer class which writes a final XML file with Jaxb2Marshaller, with the following code:
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<MyFile> 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);
}
The beginning of the XSD looks this:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" attributeFormDefault="unqualified"
targetNamespace="http://xmlns.myproject.com/v1"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://xmlns.myproject.com/v1">
And the beginning of the final generated file looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<eExact xmlns="http://xmlns.myproject.com/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="myxsd.xsd">
And what I want:
I want to remove from the final file the xmlns="http://xmlns.myproject.com/v1"
part, but be able to validate the final file with the XSD.
Is it possible?