package com.inspect.base.core.utils.xml;

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

public class GenXmlByBean<T> {
    public String getXml(T t) {
        JAXBContext context = null;
        StringWriter sw = new StringWriter();

        try {
            context = JAXBContext.newInstance(new Class[]{t.getClass()});
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty("jaxb.encoding", "UTF-8");
            marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);
            marshaller.marshal(t, sw);
        } catch (JAXBException e) {
            e.printStackTrace();
        }

        return sw.toString();
    }
}
