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



我有一个完美的解决方案,可以将对象封接到最终的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 也使用双引号?

如果你的工厂是伍德斯托克。

XMLOutputFactory xif = XMLOutputFactory.newFactory();
 xif.setProperty("com.ctc.wstx.useDoubleQuotesInXmlDecl","true");

最新更新