删除XML声明的一部分,编码=“ UTF-8” Standalone =“ yes” JAXB

时间:2019-05-31 12:46:09

标签: java xml jaxb java-io

我正在使用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);

1 个答案:

答案 0 :(得分:0)

我实际上正在寻找完全相同的东西,这就是我发现的 Altering the XML header produced by the JAXB marshaller

相关问题