cn.acyou.leo.framework.util.XMLUtil Maven / Gradle / Ivy
package cn.acyou.leo.framework.util;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
/**
* @author youfang
* @version [1.0.0, 2023/5/25 16:01]
**/
@Slf4j
public class XMLUtil {
public static XmlMapper xmlMapper = new XmlMapper();
static {
xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
xmlMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
xmlMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
xmlMapper.enable(MapperFeature.USE_STD_BEAN_NAMING);
}
/**
* 将xml转为bean对象
*
* @param input XML字符串
* @return Bean对象
* @throws IOException 异常
*/
public static T xmlToBean(String input, Class cls) throws IOException {
return xmlMapper.readValue(input, cls);
}
/**
* 将bean转为xml字符串,bean需要配置注解@JacksonXmlProperty等。
*
* @param input Bean对象
* @return XML字符串
*/
public static String beanToXmlStr(Object input) throws IOException {
return xmlMapper.writeValueAsString(input);
}
/**
* 将bean的xml字符串转为map,bean需要配置注解@JacksonXmlProperty等。
*
* @param input 输入
* @return {@link Map}<{@link String}, {@link Object}>
* @throws IOException 异常
*/
public static Map beanToXmlStrToMap(Object input) throws IOException {
String xmlStr = xmlMapper.writeValueAsString(input);
return xmlMapper.readValue(xmlStr, new TypeReference