Java XML JAXB Marshaller marshal(Object object)

Here you can find the source of marshal(Object object)

Description

marshal

License

Open Source License

Declaration

public static String marshal(Object object) throws Exception 

Method Source Code

//package com.java2s;
/*//  w w w . j  a  va  2  s.c o m
 * Copyright (c) 2004-2011 Marco Maccaferri and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Marco Maccaferri - initial API and implementation
 */

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

public class Main {
    public static String marshal(Object object) throws Exception {
        StringWriter string = new StringWriter();
        JAXBContext jaxbContext = JAXBContext
                .newInstance(object.getClass());
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                Boolean.FALSE);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); //$NON-NLS-1$
        marshaller.marshal(object, string);
        return string.toString();
    }
}

Related

  1. marshal(Object obj)
  2. marshal(Object obj, Class clazz)
  3. marshal(Object obj, OutputStream out)
  4. marshal(Object obj, OutputStream out, Class... boundClasses)
  5. marshal(Object obj, OutputStream stream)
  6. marshal(Object object)
  7. marshal(Object object)
  8. marshal(Object object, File file, JAXBContext ctx)
  9. marshal(Object object, OutputStream stream)