Десериализовать пустой элемент XML с помощью XStream - PullRequest
5 голосов
/ 03 февраля 2012

В потоке XML, который я получаю, у меня есть следующий оператор

<user>
    <age/>
</user>

, который должен быть вставлен в объект, который выглядит следующим образом:

@XStreamAlias("user")
public class User {

    public int age = 0;
}

К сожалению, я получаюИсключения XStream каждый раз, когда я пытаюсь прочитать этот XML, так как тег age 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 рассматривать это поле как «необязательное» или «возможно, не содержащее»?*

1 Ответ

2 голосов
/ 03 февраля 2012

К сожалению, это не так просто, как могло бы быть. Есть два способа сделать это:

  • Напишите преобразование с помощью xslt и примените его к потоку, прежде чем читать его с помощью XStream, чтобы xml соответствовал вашим Java Beans или
  • Напишите свой JavaBeanConverter и зарегистрируйте его в XStream. Таким образом, вы можете детально определить, как ваш xml будет отображаться на ваш Java Beans. Вы можете найти подсказку о том, как зарегистрировать JavaBeanConverter с помощью XStream в этом вопросе.
...