Here is my code :
@XStreamAlias("transaction")
public class Student {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
public static void main(String[] args) {
String xml1 = "<transaction><name>xiaoming</name><age>20</age> </transaction>" ;
XStream xstream = new XStream(new DomDriver());
xstream.processAnnotations(Student.class);
Student stu1 = (Student)xstream.fromXML(xml1);
System.out.println(stu1);
String xml2 = "<transaction><name>xiaoming</name><age/></transaction>" ;
xstream.processAnnotations(Student.class);
Student stu2 = (Student)xstream.fromXML(xml2);
System.out.println(stu2);
}
}
I can convert xml1 to stu1, but when I convert xml2 to stu2, I got the error message like this :
Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: Zero length string : Zero length string
---- Debugging information ----
message : Zero length string
cause-exception : java.lang.NumberFormatException
cause-message : Zero length string
class : java.lang.Integer
required-type : java.lang.Integer
converter-type : com.thoughtworks.xstream.converters.SingleValueConverterWrapper
wrapped-converter : com.thoughtworks.xstream.converters.basic.IntConverter
path : /transaction/age
class[1] : Student
converter-type[1] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
version : 1.4.7
-------------------------------
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:474)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:406)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:257)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
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:1185)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1169)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1040)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1031)
at TestJsonConvert.main(TestJsonConvert.java:20)
Caused by: java.lang.NumberFormatException: Zero length string
at java.lang.Long.decode(Long.java:893)
at com.thoughtworks.xstream.converters.basic.IntConverter.fromString(IntConverter.java:27)
at com.thoughtworks.xstream.converters.SingleValueConverterWrapper.fromString(SingleValueConverterWrapper.java:41)
at com.thoughtworks.xstream.converters.SingleValueConverterWrapper.unmarshal(SingleValueConverterWrapper.java:49)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
... 16 more
You need to write your own converter to handle empty tags:
public class IntConverter extends AbstractBasicConverter {
public boolean canConvert(Class type) {
return type.equals(int.class) || type.equals(Integer.class);
}
protected Object fromString(String str) {
/* If empty tag. */
if (str == null || str.trim().length == 0) {
/* Default to zero. */
str = "0";
}
return Integer.decode(str);
}
}
Register the converter as follows:
xstream.registerConverter(new IntConverter());