Java XML JAXB Marshaller marshal(Object bean)

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

Description

marshal

License

Open Source License

Declaration

public static String marshal(Object bean) throws JAXBException 

Method Source Code


//package com.java2s;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import java.io.OutputStream;

import java.io.StringWriter;

public class Main {
    private static JAXBContext jaxbContext;
    private static ThreadLocal<Marshaller> marshaller = new ThreadLocal<Marshaller>();

    public static String marshal(Object bean) throws JAXBException {
        StringWriter writer = new StringWriter();
        getMarshaller().marshal(bean, writer);
        return writer.toString();
    }//  ww  w .  j  a v a 2 s  . co m

    public static void marshal(Object bean, OutputStream os) throws JAXBException {
        getMarshaller().marshal(bean, os);
    }

    private static Marshaller getMarshaller() throws JAXBException {
        Marshaller m = marshaller.get();
        if (m == null) {
            m = jaxbContext.createMarshaller();
            marshaller.set(m);
        }
        return m;
    }
}

Related

  1. marshal(JAXBElement jaxbElement, Class cls)
  2. marshal(JAXBElement value, String contextPath, OutputStream out, ClassLoader classLoader)
  3. marshal(Marshaller m, File out, Object o)
  4. marshal(Marshaller marshaller, Object object, String filename)
  5. marshal(Marshaller marshaller, Object object, String filename)
  6. marshal(Object entity)
  7. marshal(Object obj)
  8. marshal(Object obj)
  9. marshal(Object obj)