2012-02-03 21 views
5

का उपयोग कर एक XML धारा मैं प्राप्त होता है तत्व, मैं निम्नलिखित बयानdeserialize खाली एक्सएमएल Xstream

<user> 
    <age/> 
</user> 

जो एक उद्देश्य यह है कि इस तरह दिखता है पर डालने की है:

@XStreamAlias("user") 
public class User { 

    public int age = 0; 
} 

दुर्भाग्य से, जब भी मैं इस एक्सएमएल को पढ़ने की कोशिश करता हूं, तो मुझे XStream अपवाद प्राप्त होते हैं, क्योंकि उम्र xml टैग खाली है:

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) 

मैं टेलिफोन कैसे कर सकता हूं एल XStream इस क्षेत्र को "वैकल्पिक" या "possibliy कुछ भी नहीं" के रूप में विचार करने के लिए?

उत्तर

2

असुरक्षित रूप से यह जितना आसान हो सकता है उतना आसान नहीं है। वहाँ दो तरीके है कि आप कर सकते हैं:

  • एक परिवर्तन xslt के साथ इतना है कि एक्सएमएल अपने जावा बीन्स या
  • से मेल खाता लिखें और धारा में इसे लागू करने से पहले उसे Xstream के साथ पठित अपनी खुद लिखें JavaBeanConverter और इसे एक्सस्ट्रीम के साथ पंजीकृत करें। इस तरह आप विस्तार से परिभाषित कर सकते हैं कि आपके एक्सएमएल को आपके जावा बीन्स में कैसे मैप किया जा रहा है। आप XBream के साथ JavaBeanConverter को this प्रश्न पर पंजीकृत करने के तरीके पर एक संकेत प्राप्त कर सकते हैं।
संबंधित मुद्दे