【问题标题】:Deserialize empty XML element using XStream使用 XStream 反序列化空 XML 元素
【发布时间】:2012-02-25 20:35:22
【问题描述】:

在我收到的 XML 流中,我有以下语句

<user>
    <age/>
</user>

要插入的对象如下所示:

@XStreamAlias("user")
public class User {

    public int age = 0;
}

不幸的是,我每次尝试读取此 XML 时都会收到 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 将此字段视为“可选”或“可能不包含任何内容”?

【问题讨论】:

    标签: java xml deserialization xstream


    【解决方案1】:

    不幸的是,这并不容易。有两种方法可以做到这一点:

    • 使用 xslt 编写转换并将其应用于流,然后再使用 XStream 读取它,以便 xml 匹配您的 Java Bean 或
    • 编写您自己的JavaBeanConverter 并将其注册到XStream。通过这种方式,您可以详细定义您的 xml 将如何映射到您的 Java Bean。您可以在this 问题上找到有关如何使用 XStream 注册 JavaBeanConverter 的提示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 2015-04-13
      • 2011-02-27
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      相关资源
      最近更新 更多