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);


你可能感兴趣的:(xml 报文转对象方法)