package org.jeecg.modules.common.util;

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

/**
 * 普通的java bean转XML格式字符串数据
 */
public class XmlUtil {

    /**
     * java对象转换为xml文件
     *
     * @return xml文件的String
     * @throws JAXBException
     */
    public static String beanToXml(Object obj, String charset) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, charset);
        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        return writer.toString();
    }




}
