2012-02-03 17 views
5

En una secuencia XML que recibo, que tiene la siguiente declaraciónDeserialize elemento XML vacío usando xstream

<user> 
    <age/> 
</user> 

la que se va a insertar en un objeto que se parece a esto:

@XStreamAlias("user") 
public class User { 

    public int age = 0; 
} 

Desafortunadamente, recibo excepciones XStream cada vez que trato de leer este XML, como la etiqueta XML edad está vacía:

Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: For input string: "" : For input string: "" 
---- Debugging information ---- 
message    : For input string: "" 
cause-exception  : java.lang.NumberFormatException 
cause-message  : For input 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    : /GoodreadsResponse/user/age 
line number   : 17 
class[1]   : fr.riduidel.exporter.goodreads.User 
converter-type[1] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter 
class[2]   : fr.riduidel.exporter.goodreads.GoodreadsResponse 
version    : null 
------------------------------- 
    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:355) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234) 
    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.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:322) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234) 
    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:1052) 
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1036) 
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:921) 

Cómo puedo tel l XStream para considerar este campo como "opcional" o "posible que no contenga nada"?

Respuesta

2

Desafortunadamente, esto no es tan fácil como podría ser. Hay dos maneras de hacer que usted podría:

  • Escribe una transformación con XSLT y aplicarlo a la corriente antes de leerlo con xstream de manera que el código XML que corresponda a los Java Beans o
  • escribir sus propios JavaBeanConverter y registrarlo con XStream. De esta forma, puede definir en detalle cómo su xml se asignará a sus Java Beans. Puede encontrar una pista sobre cómo registrar el JavaBeanConverter con XStream en la pregunta this.
Cuestiones relacionadas