package com.example.xml.hgMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@Slf4j
public class XmlUtils {
/**
* 对象转换为Xml格式数据
*
* @param obj 对象
* @param coding 编码
* - UTF-8:适用于多语言环境,支持Unicode字符集。
* - UTF-16:同样适用于多语言环境,支持Unicode字符集,但相对于UTF-8会占用更多的存储空间。
* - ISO-8859-1:适用于拉丁字母语言,如英语、法语、德语等。
* - GB2312:适用于简体中文。
* @return Xml格式
*/
public static String convertToXml(Object obj, String coding) {
StringBuilder xmlBuilder = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
xmlBuilder.append(convertToXmlData(obj, StringUtils.isEmpty(coding) ? coding : "UTF-8"));
return xmlBuilder.toString();
}
public static String convertToXmlData(Object obj, String encoding) {
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
return writer.toString();
} catch (Exception e) {
log.error("转换XML失败:" + e.getMessage());
return null;
}
}
/**
* 字符串转换为Xml文件进行输出
*
* @param str 字符串
* @param path Xml文件输出地址
* @return
*/
public static void convertToXmlFile(String str, String path) {
Path filePath = Paths.get(path);
try {
Files.createDirectories(filePath.getParent());
Files.write(filePath, str.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Xml字符串转换成任意对象
*
* @param clazz 结果对象存储结构
* @param xmlStr Xml字符串
* @return 转换后的结果接收对象
*/
@SuppressWarnings("unchecked")
public static Object convertXmlStrToObject(Class clazz, String xmlStr) {
//String resultXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
Object xmlObject = null;
try {
JAXBContext context = JAXBContext.newInstance(clazz);
// 进行将Xml转成对象的核心接口
Unmarshaller unmarshaller = context.createUnmarshaller();
//StringReader sr = new StringReader(resultXml + xmlStr);
StringReader sr = new StringReader(xmlStr);
xmlObject = unmarshaller.unmarshal(sr);
} catch (JAXBException e) {
e.printStackTrace();
}
return xmlObject;
}
/**
* 读取file类型的xml文件转换成对象
*
* @param clazz 结果对象存储结构
* @param xmlPath XmlFile文件地址
* @return 转换后的结果接收对象
*/
@SuppressWarnings("unchecked")
public static Object convertXmlFileToObject(Class clazz, String xmlPath) {
Object xmlObject = null;
try {
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = context.createUnmarshaller();
File xmlFile = new File(xmlPath);
xmlObject = unmarshaller.unmarshal(xmlFile);
} catch (JAXBException e) {
e.printStackTrace();
}
return xmlObject;
}
/**
* 读取指定路径的xml文件转换为Java对象
*
* @param xmlPath Xml文件地址
* @param load 结果对象存储结构
* @return 转换后的结果接收对象
*/
public static Object xmlToBean(String xmlPath, Class<?> load) {
JAXBContext context = null;
Object object = null;
try {
context = JAXBContext.newInstance(load);
Unmarshaller unmarshaller = context.createUnmarshaller();
object = unmarshaller.unmarshal(new File(xmlPath));
} catch (JAXBException e) {
e.printStackTrace();
}
return object;
}
}