xstream.fromXML转换错误

J2EE 码拜 10年前 (2015-04-04) 3203次浏览 0个评论
 
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是个列表,
我估计是类的定义没有定义对,但第一次弄这个转换,搞了好久没搞对,哪位有解决方法 ???

xstream.fromXML转换错误
10分
貌似因为你的item是一个集合,而你配置的xml里面item看不出是集合吧
xstream.fromXML转换错误
30分
http://blog.csdn.net/simonchi/article/details/38926817  这篇文章讲的就是xstream和java对象之间的转换,很适合你!

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明xstream.fromXML转换错误
喜欢 (1)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!