Java对象与xml字符串转换
2021-10-08 本文已影响0人
ShrJanLan
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
public class XMLUtil {
/**
* 将对象转成xml字符串
* @param obj
* @return
*/
public static String convertToXml(Object obj) {
StringWriter sw = new StringWriter();
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.marshal(obj, sw);
} catch (JAXBException e) {
e.printStackTrace();
}
return sw.toString();
}
/**
* 将xml字符串转成对象
* @param clazz
* @param xmlStr
* @return
*/
public static Object convertXmlStrToObject(Class<?> clazz, String xmlStr) {
Object xmlObject = null;
try {
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller unmarshal = context.createUnmarshaller();
StringReader sr = new StringReader(xmlStr);
xmlObject = unmarshal.unmarshal(sr);
} catch (Exception e) {
e.printStackTrace();
}
return xmlObject;
}
}