Java XML JAXB Marshaller marshal(final Object obj, final Class... classes)

Here you can find the source of marshal(final Object obj, final Class... classes)

Description

JAXBContext.newInstance(..) is a slow operation.

License

Open Source License

Declaration

public static String marshal(final Object obj, final Class... classes) throws JAXBException 

Method Source Code

//package com.java2s;
/*//ww  w .jav  a  2  s . co m
 * Copyright (2009) Schibsted ASA
 *   This file is part of Sesat Commons.
 *
 *   Sesat Commons is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Sesat Commons 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 Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Sesat Commons.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

public class Main {
    /** JAXBContext.newInstance(..) is a slow operation.
     * @deprecated It is better the client creates the JAXBContent once and re-uses it to the other marshal method.
     */
    public static String marshal(final Object obj, final Class... classes) throws JAXBException {
        return marshal(JAXBContext.newInstance(classes), obj);
    }

    public static String marshal(final JAXBContext context, final Object obj) throws JAXBException {

        final StringWriter out = new StringWriter();
        context.createMarshaller().marshal(obj, out);
        return out.toString();
    }
}

Related

  1. marshal(Class clz, T marshalObj)
  2. marshal(Class c, String xml)
  3. marshal(Class c, String xml)
  4. marshal(Class clazz, T obj)
  5. marshal(Class clazz, T object)
  6. marshal(final Object object)
  7. marshal(final Object object)
  8. marshal(JAXBContext context, Object object, Writer writer, Map properties)
  9. marshal(JAXBElement e, File f)