2017-09-08 198 views
0

我正在使用以下代碼使用JAXB創建XML,但創建XML時不包括XML標頭。JAXB - 忽略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

這是我用我的工作,它的工作原理

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(); 
    } 
+0

這爲我工作。非常感謝! – KhadBr

+0

@KhadBr歡迎你的朋友;) –