Java XML JAXB Serialize saveDataToFile(File file, T data)

Here you can find the source of saveDataToFile(File file, T data)

Description

Saves the data in the file in xml format.

License

Open Source License

Parameter

Parameter Description
file Points to a valid xml file containing data that match the classToConvert . Cannot be null.

Exception

Parameter Description
FileNotFoundException Thrown if the file is missing.
JAXBException Thrown if there is an error during converting the datainto xml and writing to the file.

Declaration

public static <T> void saveDataToFile(File file, T data) throws FileNotFoundException, JAXBException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileNotFoundException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Main {
    /**//  w  w w . j a v  a 2s.  c o m
     * Saves the data in the file in xml format.
     *
     * @param file Points to a valid xml file containing data that match the {@code classToConvert}.
     *             Cannot be null.
     * @throws FileNotFoundException Thrown if the file is missing.
     * @throws JAXBException         Thrown if there is an error during converting the data
     *                               into xml and writing to the file.
     */
    public static <T> void saveDataToFile(File file, T data) throws FileNotFoundException, JAXBException {

        assert file != null;
        assert data != null;

        if (!file.exists()) {
            throw new FileNotFoundException("File not found : " + file.getAbsolutePath());
        }
        JAXBContext context = JAXBContext.newInstance(data.getClass());
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(data, file);
    }
}

Related

  1. deserializeFile(String path, Class clazz)
  2. deserializeFromXmlFile(T defaultObject, String fileName)
  3. deserializeXmlToJava(String valueType, Serializable value)
  4. save(Class confClass, Object confObj, File xmlFile)
  5. save(File file, T obj, Class... clazz)
  6. saveModel(JAXBElement model, String packageName)
  7. saveObject(Path path, T object)
  8. saveObject(T object, Class typeClass, URL path)
  9. saveToFile(String fileName, T obj)