xtream 对象转XML成功,但再将转换后的XML转对象时出错

无爱尽头 发布于 2016/09/04 14:57
阅读 516
收藏 0


//根节点


@XStreamAlias("LaneInfo")
public class LaneInfo {


@XStreamAlias("Volumes")
@XStreamImplicit(itemFieldName = "Volumes")
    private List<Volumes> Volumes;


public List<Volumes> getVolumes() {
return Volumes;
}
public void setVolumes(List<Volumes> volumes) {
Volumes = volumes;
}


}


//孩子节点,有多个
@XStreamAlias("Volumes")
public class Volumes {
@XStreamAlias("Volume")
    private Volume Volume;


public Volume getVolume() {
return Volume;
}


public void setVolume(Volume volume) {
Volume = volume;
}
    
}




//各孩子节点中有一个孙子节点,节点有值
//该节点有一个名为class的属性
@XStreamConverter(value=ToAttributedValueConverter.class,strings={"content"})
public class Volume {
@XStreamAsAttribute
@XStreamAlias("class")
private String class1;
private String content;

public Volume (String cls,String val){
this.class1=cls;
this.content=val;
}


public String getClass1() {
return class1;
}
public void setClass1(String class1) {
this.class1 = class1;
}


public String getContent() {
return content;
}


public void setContent(String content) {
this.content = content;
}

}


//main class
public class T {


public static void main(String[] args) {
LaneInfo lInfo=new LaneInfo();
List<Volumes> volumes=new ArrayList<Volumes>();

Volume vl1=new Volume("1","10");
Volume vl2=new Volume("2","20");
Volume vl3=new Volume("3","30");

Volumes v1=new Volumes();
v1.setVolume(vl1);
volumes.add(v1);

Volumes v2=new Volumes();
v2.setVolume(vl2);
volumes.add(v2);

Volumes v3=new Volumes();
v3.setVolume(vl3);
volumes.add(v3);

lInfo.setVolumes(volumes);

XStream xStream = new XStream(new DomDriver());
xStream.processAnnotations(LaneInfo.class);
String xml=xStream.toXML(lInfo);
System.out.println("xml is: "+xml);
List<Class> classes = new ArrayList<Class>();
classes.add(LaneInfo.class);

LaneInfo laneInfo=new LaneInfo();
laneInfo=(LaneInfo)xStream.fromXML(xml);


}
}




//后台输出,obj2xml成功,xml2obj失败,请问应该怎么处理才能将该结构的XML转成OBJ??  
xml is: <LaneInfo>
  <Volumes>
    <Volume class="1">10</Volume>
  </Volumes>
  <Volumes>
    <Volume class="2">20</Volume>
  </Volumes>
  <Volumes>
    <Volume class="3">30</Volume>
  </Volumes>
</LaneInfo>
Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: 
---- Debugging information ----
cause-exception     : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : 1
class               : com.SerializableBeans.Volumes
required-type       : com.SerializableBeans.Volumes
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /LaneInfo/Volumes/Volume
class[1]            : com.SerializableBeans.LaneInfo
version             : 1.4.9
-------------------------------
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:449)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1230)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1214)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1085)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1076)
at com.util.T.main(T.java:47)
Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: 1
at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:81)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.SecurityMapper.realClass(SecurityMapper.java:71)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:47)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:424)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
... 16 more



加载中
返回顶部
顶部