com.github.nicklaus4.webservice.utils.XmlUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webservice-toolkit Show documentation
Show all versions of webservice-toolkit Show documentation
friendly common pools base on spring-boot
The newest version!
package com.github.nicklaus4.webservice.utils;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author weishibai
* @date 2019/05/02 1:49 PM
*/
public class XmlUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(XmlUtils.class);
public static String toXml(T obj, Class clz) {
try {
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(clz);
Marshaller marshal = context.createMarshaller();
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // 格式化输出
marshal.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// 编码格式,默认为utf-8
marshal.setProperty(Marshaller.JAXB_FRAGMENT, false);// 是否省略xml头信息
marshal.setProperty("jaxb.encoding", "utf-8");
marshal.marshal(obj, writer);
return new String(writer.getBuffer());
} catch (JAXBException e) {
LOGGER.error("xml parse error: ", e);
return "";
}
}
public static T fromXml(String xmlStr, Class clz) {
try {
JAXBContext context = JAXBContext.newInstance(clz);
Unmarshaller unmarshaller = context.createUnmarshaller();
//noinspection unchecked
return (T) unmarshaller.unmarshal(new StringReader(xmlStr));
} catch (JAXBException e) {
LOGGER.error("parse from xml error: ", e);
return null;
}
}
}