Java XML JAXB Serialize serialize(Object obj)

Here you can find the source of serialize(Object obj)

Description

serialize

License

Open Source License

Declaration

public static String serialize(Object obj) 

Method Source Code

//package com.java2s;
/**/*from   ww w .ja  va  2s. c o m*/
 * This file is part of aion-lightning <aion-lightning.org>.
 * 
 * aion-lightning 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 3 of the License, or
 * (at your option) any later version.
 * 
 * aion-lightning 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 aion-lightning.  If not, see <http://www.gnu.org/licenses/>.
 */

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

import java.io.StringWriter;

public class Main {
    public static String serialize(Object obj) {
        try {
            JAXBContext jc = JAXBContext.newInstance(obj.getClass());
            Marshaller m = jc.createMarshaller();
            m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            StringWriter sw = new StringWriter();
            m.marshal(obj, sw);
            return sw.toString();
        } catch (JAXBException e) {
            throw new RuntimeException("Failed to marshall object of class " + obj.getClass().getName(), e);
        }
    }
}

Related

  1. serialize(JAXBElement emp, Class clazz, OutputStream out)
  2. serialize(JAXBElement object)
  3. serialize(Object o, OutputStream os, Boolean format)
  4. serialize(Object o, OutputStream os, Boolean format)
  5. serialize(Object obj)
  6. serialize(Object object, Class clazz, String filename)
  7. serialize(T object, Class objectClass, OutputStream resultStream)
  8. serialize(T object, Path path)
  9. serializeFile(String path, Object o)