Here you can find the source of saveXML(Object object, String fileNamePath, Boolean append)
public static void saveXML(Object object, String fileNamePath, Boolean append)
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { public static void saveXML(Object object, String fileNamePath, Boolean append) { Marshaller marshaller = createMarshaller(object); File file = createFile(fileNamePath); FileWriter fileWriter;//www.j a va 2s . c o m try { fileWriter = new FileWriter(file, append); if (append) { fileWriter.append(System.getProperty("line.separator")); } marshaller.marshal(object, fileWriter); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void saveXML(Object object, String fileNamePath) { saveXML(object, fileNamePath, false); } public static void saveXML(Object object) { Marshaller marshaller = createMarshaller(object); try { marshaller.marshal(object, System.out); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static Marshaller createMarshaller(Object object) { JAXBContext context; Marshaller marshaller = null; try { context = JAXBContext.newInstance(object.getClass()); marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } return marshaller; } private static File createFile(String fileNamePath) { File file = new File(fileNamePath); if (!file.exists()) { file.getParentFile().mkdirs(); try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return file; } }