2015-12-24 7 views
0

Вот мой код:как я могу решить нулевую строку исключения длины в 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 

ответ

1

Вы должны написать свой собственный конвертер обрабатывать пустые теги:

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); 
    } 
} 

Зарегистрируйте преобразователь следующим образом:

xstream.registerConverter(new IntConverter()); 
+0

спасибо за вашу помощь; Я хотел бы знать, к какой jar AbstractBasicConverter принадлежит, это правильно?
\t \t org.compass \t \t компас \t \t 1,1 \t \t lynnsea

+0

нормально, я уже решил эту программу; спасибо за приглашение; – lynnsea

1
//firstly add IntConverter Class; 
public class IntConverter implements Converter { 
@Override 
public boolean canConvert(Class clazz) { 
    return clazz.equals(Integer.class); 
} 

@Override 
public void marshal(Object Obj, HierarchicalStreamWriter arg1, MarshallingContext arg2) { 

} 

@Override 
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { 
    String value = reader.getValue(); 
    if(StringUtils.isEmpty(value)){ 
     return null; 
    } 
    return Integer.valueOf(value); 
} 
} 
//secondly , register IntConverter when xstream convert String; 
xstream.registerConverter((Converter) new IntConverter());