2012-02-03 35 views
5

Trong một dòng XML tôi nhận được, tôi có tuyên bố sauDeserialize trống XML yếu tố sử dụng xStream

<user> 
    <age/> 
</user> 

mà là để được đưa vào trong một đối tượng mà trông như thế này:

@XStreamAlias("user") 
public class User { 

    public int age = 0; 
} 

Thật không may, Tôi nhận được ngoại lệ XStream mỗi khi tôi cố đọc XML này, vì thẻ tuổi xml trống:

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) 

Làm cách nào tôi có thể gọi l XStream để xem xét lĩnh vực này là "tùy chọn" hoặc "possibliy không có gì"?

Trả lời

2

May mắn thay, điều này không dễ dàng như vậy. Có hai cách để làm điều đó bạn có thể:

  • Viết chuyển đổi với XSLT và áp dụng nó vào dòng trước khi bạn đọc nó với xStream để xml phù hợp Beans Java của bạn hoặc
  • Viết riêng bạn JavaBeanConverter và đăng ký nó với XStream. Bằng cách này bạn có thể xác định chi tiết cách xml của bạn sẽ được ánh xạ tới Hạt Java của bạn. Bạn có thể tìm thấy gợi ý về cách đăng ký JavaBeanConverter với XStream theo câu hỏi this.
Các vấn đề liên quan