Java XML JAXB Serialize xmlSerialize(String filename, T obj)

Here you can find the source of xmlSerialize(String filename, T obj)

Description

Serialize an object to file as XML.

License

Open Source License

Parameter

Parameter Description
T a XML serializable type
filename the file to which the serialization is written
obj the serializable object

Exception

Parameter Description

Declaration

public static <T> void xmlSerialize(String filename, T obj) throws JAXBException, FileNotFoundException 

Method Source Code

//package com.java2s;
/*//from w ww  . java  2s . c o m
 * Copyright (C) 2015 Dieter J Kybelksties
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * @date: 2015-12-16
 * @author: Dieter J Kybelksties
 */

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

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

public class Main {
    /**
     * Serialize an object to file as XML.
     *
     * @param <T>      a XML serializable type
     * @param filename the file to which the serialization is written
     * @param obj      the serializable object
     * @throws javax.xml.bind.JAXBException
     * @throws java.io.FileNotFoundException
     */
    public static <T> void xmlSerialize(String filename, T obj) throws JAXBException, FileNotFoundException {
        FileOutputStream fileOut = new FileOutputStream(filename);
        JAXBContext context = JAXBContext.newInstance(obj.getClass());

        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        m.marshal(obj, fileOut);
    }
}

Related

  1. serializeToFile(Class clazz, T object, String outputFile)
  2. serializeToSTD(Object obj)
  3. serializeToString(Object obj)
  4. serializeToString(Object obj)
  5. serializeToXmlFile(T object, String fileName)