日期:2014-05-19  浏览次数:20798 次

XStream解析XML的问题
<presons>
<preson>
<name>ffff</name>
<sex>x</sex>
<age>1</age>
</preson>
<preson>
<name>rrrrr</name>
<sex>f</sex>
<age>4</age>
</preson>
</presons>
这样的一个XML,我定义了2个javaBean:

public class Presons {
private List<Preson> preson;

public List<Preson> getPreson() {
return preson;
}

public void setPreson(List<Preson> preson) {
this.preson = preson;
}

public class Preson {
private String name;
private String sex;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}

}

解析的时候是这样的:
XStream xs = new XStream(new DomDriver());
xs.alias("presons", Presons.class);
xs.alias("preson", Preson.class);

xs.fromXML(msg);
报错了:
com.thoughtworks.xstream.converters.ConversionException: name : name : name : name
---- Debugging information ----
message : name : name
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : name : name
class : com.suning.epp.seam.test.Presons
required-type : java.util.ArrayList
path : /presons/preson/name
-------------------------------


但是很奇怪的是,如果我仅仅解析<preson>标签,是可以的:
String msg1 = "<preson><name>ffff</name><sex>x</sex><age>1</age></preson>";
XStream xs = new XStream(new DomDriver());
//xs.alias("presons", Presons.class);
xs.alias("preson", Preson.class);

Preson p = (Preson)xs.fromXML(msg1);
这个就是对的。
还请达人帮忙看一下,很急的,谢谢

------解决方案--------------------
google xstream addImplicitCollection