package com.matrix.system.common.tools;
|
|
import javax.xml.bind.JAXBContext;
|
import javax.xml.bind.Unmarshaller;
|
import java.io.StringReader;
|
|
/**
|
* @Author: pengliang
|
* @Date: 2019-06-18
|
* 解析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;
|
}
|
}
|