我正在使用 JAXB 拆分传入的 xml 文件。 我知道这看起来像是一个重复的线程,但是
预期,
<?xml version="1.0"?>
实际
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
我尝试使用 JAXB_FRAGMENT 来完全删除第一行
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
和
jaxbMarshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
有和没有Marshaller.JAXB_FRAGMENT
仅供引用,我正在使用 StringWriter
JAXBContext jaxbContext = JAXBContext.newInstance(ExportDocument.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);//
//jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
//jaxbMarshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(employee, sw);
String xmlContent = sw.toString();
System.out.println(xmlContent);
最佳答案
我实际上在寻找完全相同的东西,这就是我找到的 Altering the XML header produced by the JAXB marshaller
关于java - 删除部分 XML 声明,编码 ="UTF-8"独立 ="yes"JAXB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56395384/