错误原因
发来的数据和定义的类标签名称不匹配
无法解析
错误分析
- 看看自己的XmlRootElement写了name属性没有,没写的加上
- 如果写了看看和发过来的数据的根标签是不是匹配
这里就不匹配了,发过来的是root,name是skuInfo
所有才会报错
错误解决
改过来即可
成功解析
附:xml解析代码
public class XMLUtil {
@SuppressWarnings("unchecked")
public static <T> T xmlToBean(String xmlPath, Class<T> load) throws Exception {
JAXBContext context = JAXBContext.newInstance(load);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (T) unmarshaller.unmarshal(new StringReader(xmlPath));
}
}