Вот мой код:как я могу решить нулевую строку исключения длины в XStream
@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);
}
}
можно преобразовать xml1 в stu1, но когда я конвертировать xml2 в STU2, я получил сообщение об ошибке, как это :
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
спасибо за вашу помощь; Я хотел бы знать, к какой jar AbstractBasicConverter принадлежит, это правильно?
\t \t org.compass \t \t компас \t \t 1,1 \t \t –
lynnsea
нормально, я уже решил эту программу; спасибо за приглашение; – lynnsea