前端之家收集整理的这篇文章主要介绍了
xml 报文转对象方法,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class XmlTransTools {
public static<T> T Xml2JBean(String sourceXml,Class<T> target) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(target);
Unmarshaller unmarshaller = context.createUnmarshaller();
@SuppressWarnings("unchecked")
T result = (T) unmarshaller.unmarshal(new StringReader(sourceXml));
return result;
}
}
WeixinResponse weixinResponse = XmlTransTools.Xml2JBean(xmlParam,WeixinResponse.class);