2012-02-03 6 views
5

In un flusso XML che ricevo, ho la seguente dichiarazioneDeserialize elemento XML vuoto utilizzando xstream

<user> 
    <age/> 
</user> 

, che deve essere inserita in un oggetto che assomiglia a questo:

@XStreamAlias("user") 
public class User { 

    public int age = 0; 
} 

Purtroppo, ricevo eccezioni XStream ogni volta che provo a leggere questo XML, come il tag di età xml è vuoto:

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) 

come posso tel l XStream considera questo campo come "facoltativo" o "possibilmente non contenente nulla"?

risposta

2

Unforunately questo non è facile come potrebbe essere. Ci sono due modi per farlo che si potrebbe:

  • Scrivi una trasformazione con XSLT e applicarlo al flusso prima di leggere con xstream in modo che il codice XML soddisfa le vostre Java Beans o
  • scrivere il proprio JavaBeanConverter e registrarlo con XStream. In questo modo puoi definire in dettaglio come il tuo xml verrà mappato ai tuoi Java Beans. È possibile trovare un suggerimento su come registrare JavaBeanConverter con XStream nella domanda this.