У меня есть отлично работающий класс записи файлов XML, который записывает окончательный файл XML с помощью Jaxb2Marshaller со следующим кодом:
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);
}
Начало XSD выглядит так:
<?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">
И начало окончательного сгенерированного файла выглядит так:
<?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">
И что я хочу:
Я хочу удалить из окончательного файла часть xmlns="http://xmlns.myproject.com/v1"
, но иметь возможность проверить окончательный файл с помощью XSD.
Возможно ли это?