public static void main(String[] args) { String str = "<SendPicsInfo><Count>2</Count>\n" + "<PicList><item><PicMd5Sum>1db014f3630bd2e7a79a39694bd11a8f</PicMd5Sum></item></PicList>\n" + "</SendPicsInfo>"; XStream xstream = Util.xstream(); xstream.alias("item", item.class); xstream.alias("PicList", PicList.class); xstream.alias("SendPicsInfo", SendPicsInfo.class); SendPicsInfo im = (SendPicsInfo)xstream.fromXML(str); System.out.println(im); } public class SendPicsInfo { protected int Count; protected PicList PicList; public int getCount() { return Count; } /** * @param Count the Count to set */ public void setCount(int Count) { this.Count = Count; } public PicList getPicList() { return PicList; } public void setPicList(PicList PicList) { this.PicList = PicList; } } public class PicList { protected List<item> item; public List<item> getItem() { return item; } public void setItem(ArrayList<item> item) { this.item = item; } } public class item { protected String PicMd5Sum; public String getPicMd5Sum() { return PicMd5Sum; } public void setPicMd5Sum(String PicMd5Sum) { this.PicMd5Sum = PicMd5Sum; } } 转换时就会报错: Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: PicMd5Sum : PicMd5Sum ---- Debugging information ---- message : PicMd5Sum cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException cause-message : PicMd5Sum class : java.util.ArrayList required-type : java.util.ArrayList converter-type : com.thoughtworks.xstream.converters.collections.CollectionConverter path : /SendPicsInfo/PicList/item/PicMd5Sum class[1] : ly.foxcw.wechat.ent.util.req.PicList converter-type[1] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter class[2] : ly.foxcw.wechat.ent.util.req.SendPicsInfo version : 1.4.7 item是个列表, |
|
10分 |
貌似因为你的item是一个集合,而你配置的xml里面item看不出是集合吧
|
30分 |
http://blog.csdn.net/simonchi/article/details/38926817 这篇文章讲的就是xstream和java对象之间的转换,很适合你!
|