Java XML JAXB Serialize saveObject(T object, Class typeClass, URL path)

Here you can find the source of saveObject(T object, Class typeClass, URL path)

Description

save Object

License

Artistic License

Declaration

public static <T> void saveObject(T object, Class<T> typeClass, URL path) 

Method Source Code


//package com.java2s;
//License from project: Artistic License 

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;

public class Main {
    public static <T> void saveObject(T object, Class<T> typeClass, URL path) {
        try {//from  w  w  w  .j  a v  a  2s.c om
            File file = new File(path.toURI());
            JAXBContext jaxbContext = JAXBContext.newInstance(typeClass);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            // output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            jaxbMarshaller.marshal(object, file);

        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    public static <T> void saveObject(T object, Class<T> typeClass, String path) {
        try {
            File file = new File(path);
            JAXBContext jaxbContext = JAXBContext.newInstance(typeClass);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            // output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            jaxbMarshaller.marshal(object, file);

        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. save(Class confClass, Object confObj, File xmlFile)
  2. save(File file, T obj, Class... clazz)
  3. saveDataToFile(File file, T data)
  4. saveModel(JAXBElement model, String packageName)
  5. saveObject(Path path, T object)
  6. saveToFile(String fileName, T obj)
  7. saveXML(Object object, String fileNamePath, Boolean append)
  8. serialiseObject(Object obj, Class objclass)
  9. serialize(final Object object)