如何在Java中使用jaxb和XMLStreamWriter在Prolog中将单引号更改为双引号,以最终XML

时间:2019-01-10 12:27:52

标签: xml spring jaxb marshalling

我有一个完美的解决方案,可以将对象编组为最终XML:

final String XSD_FILE_NAME = "my.xsd"

public void writeToFile(MyFile myFile) {
    try (OutputStream writer = new FileOutputStream(exactFilePath)) {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setSchema(resourceLoader.getResource("classpath:file/my.xsd"));
        marshaller.setContextPath("com.my.project.type");
        marshaller.setMarshallerProperties(ImmutableMap.of(
                javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE,
                javax.xml.bind.Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "my.xsd"
        ));
        marshaller.afterPropertiesSet();

        XMLStreamWriter streamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(writer, "UTF-8");
        StAXResult result = new StAXResult(streamWriter);
        marshaller.marshal(myFile, result);
        streamWriter.close();
    } catch (IOException | XMLStreamException e) {
        throw e;
    }
}

但是我意识到最终的XML文件仅对Prolog(<?xml ...>)使用单引号。但是对于根元素和子元素中的所有其他属性,请双引号:

<?xml version='1.0' encoding='UTF-8'?>
<myFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attribute1="1" attribute2="2">
  <myChild/>
</myFile>

如何设置XML Prolog也使用双引号?

0 个答案:

没有答案