xml文件解析的配置工具类
package com.test.demo.express.sf.util;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
/**
* @Author:
* @Date: 2018/10/17 23:47
* @Description: 解析xml文件
*/
public class XmlUtil {
/**
* 将xml解析成对象
* @param xml
* @param clazz
* @param <T>
* @return
*/
public static <T> T convertToObject(String xml, Class<T> clazz) {
T _clazz = null;
StringReader reader = null;
try {
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = context.createUnmarshaller();
reader = new StringReader(xml);
_clazz = (T) unmarshaller.unmarshal(reader);
} catch (Exception e) {
throw new RuntimeException(e);
}finally{
if(reader!=null){
reader.close();
}
}
return _clazz;
}
}
第一篇:https://blog.csdn.net/wildwolf_001/article/details/83410452
第二篇:https://blog.csdn.net/wildwolf_001/article/details/83411476