Java XML JAXB Marshaller marshal(T t, Class entityClass)

Here you can find the source of marshal(T t, Class entityClass)

Description

marshal

License

Open Source License

Declaration

public static <T> String marshal(T t, Class<T> entityClass) 

Method Source Code

//package com.java2s;

import java.io.StringWriter;
import java.nio.charset.StandardCharsets;

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

public class Main {
    public static <T> String marshal(T t, Class<T> entityClass) {
        StringWriter sw = new StringWriter();
        try {/*from ww w .j  a v  a 2 s. c o m*/
            JAXBContext jaxbContext = JAXBContext.newInstance(entityClass);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name());
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(t, sw);
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
        return sw.toString();
    }
}

Related

  1. marshal(Object objectToMarshal)
  2. marshal(Object source, Class configurationClass)
  3. marshal(T bean, Class... bc)
  4. marshal(T clazz)
  5. marshal(T object)
  6. marshalAsString(Class clz, T marshalObj)
  7. marshall(Class c, String xml)
  8. marshall(final Object o, Class clazz)
  9. marshall(Object o)