我使用以下代码使用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"?>
答案 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();
}