Java XML JAXB Serialize saveXML(Object object, String fileNamePath, Boolean append)

Here you can find the source of saveXML(Object object, String fileNamePath, Boolean append)

Description

save XML

License

Open Source License

Declaration

public static void saveXML(Object object, String fileNamePath, Boolean append) 

Method Source Code

//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;
    }
}

Related

  1. saveDataToFile(File file, T data)
  2. saveModel(JAXBElement model, String packageName)
  3. saveObject(Path path, T object)
  4. saveObject(T object, Class typeClass, URL path)
  5. saveToFile(String fileName, T obj)
  6. serialiseObject(Object obj, Class objclass)
  7. serialize(final Object object)
  8. serialize(JAXBElement emp, Class clazz, OutputStream out)
  9. serialize(JAXBElement object)