Java XML JAXB Marshaller marshallToString(Object jaxbElement, Class c)

Here you can find the source of marshallToString(Object jaxbElement, Class c)

Description

marshall To String

License

Open Source License

Declaration

public static <T> String marshallToString(Object jaxbElement, Class<T> c) throws JAXBException, IOException 

Method Source Code

//package com.java2s;
//License from project: GNU General Public License 

import java.io.ByteArrayOutputStream;

import java.io.IOException;

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

public class Main {
    private static JAXBContext jc = null;
    private static Marshaller m = null;

    public static <T> String marshallToString(Object jaxbElement, Class<T> c) throws JAXBException, IOException {
        initMarshaller(c);//from w  w  w  .j  av a  2 s .co  m
        ByteArrayOutputStream fos = new ByteArrayOutputStream();
        m.marshal(jaxbElement, fos);
        fos.flush();
        String res = fos.toString("UTF-8");
        fos.close();
        return res;
    }

    private static final <T> void initMarshaller(Class<T> c) throws JAXBException {
        jc = JAXBContext.newInstance(new Class[] { c });
        m = jc.createMarshaller();
    }
}

Related

  1. marshallerObject(Class c, Object o)
  2. marshallJAXBObject(String namespace, Object o)
  3. marshallMessage(Object message, Marshaller marshaller, DataOutputStream dos)
  4. marshallObjectAsString(Class clazz, T object)
  5. marshallObjectToXML(final Object jaxbModel, final URL schemaURL)
  6. marshallToXml(String zpackage, Writer writer, Object data)
  7. marshallXml(final Object object)
  8. marshallXMLInConsole(Object obj)
  9. marshalObject(Object obj, File file)