JAXB - 忽略XML标头

时间:2017-09-08 14:43:42

标签: xml jaxb

我使用以下代码使用JAXB创建XML,但是在创建XML时,不包含XML标头。

OutputStream file = new FileOutputStream("E:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(F6207.class);
javax.xml.bind.Marshaller jaxbMarshaller = jaxbContext.createMarshaller();  
jaxbMarshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8");
jaxbMarshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
jaxbMarshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true); 
jaxbMarshaller.marshal(f6207, file);

如何将以下标题放入我的XML文件中?

<?xml version="1.0" encoding="UTF-8"?>

1 个答案:

答案 0 :(得分:0)

这是我用于工作的,它有效

    JAXBContext jaxbContext;
    try {
        jaxbContext = JAXBContext.newInstance(YourPackageBeans);
        Marshaller marshaller = jaxbContext.createMarshaller();

        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));



        marshaller.marshal(YourObject, new File("E:\\file.xml"));
    } catch (JAXBException e) {
        e.printStackTrace();
    }