【问题标题】:Deserializing using Xstream使用 Xstream 反序列化
【发布时间】:2012-05-28 11:02:31
【问题描述】:

下面是我的java类

public class CRM
{

    private String phone[];
    private String email;
    public String[] getPhone()
    {
            return phone;
    }

    public void setPhone(String[] phone)
    {
            this.phone = phone;
    }

    public String getEmail()
    {
            return email;
    }

    public void setEmail(String email)
    {
            this.email = email;
    }

}

下面是我的 XML。

<Crm>
    <Phone>123456789</Phone>
    <email>a@a.com</email>
</Crm>

以下是我得到的堆栈跟踪:

Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException:     array element type mismatch : array element type mismatch
---- Debugging information ----
message             : array element type mismatch
cause-exception     : java.lang.IllegalArgumentException
cause-message       : array element type mismatch
class               : [Ljava.lang.String;
required-type       : [Ljava.lang.String;
converter-type      : com.thoughtworks.xstream.converters.collections.ArrayConverter
path                : /crm/phone
line number         : 4
class[1]            : com.CRM
converter-type[1]   : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
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.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:912)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:903)
at com.Parser.main(Parser.java:29)
Caused by: java.lang.IllegalArgumentException: array element type mismatch
at java.lang.reflect.Array.set(Native Method)
at com.thoughtworks.xstream.converters.collections.ArrayConverter.unmarshal(ArrayConverter.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
... 16 more

我是否遗漏了什么或做了什么根本错误的事情?

我正在使用 xstream-1.4.2.jar 和 Java 1.6。

我会一直尝试将 xmls 转换为 java 对象。

【问题讨论】:

    标签: xstream


    【解决方案1】:

    没有看到用于实例化 XStream 的代码,我不能 100% 确定,但问题似乎是因为在 XML 中,您提供了一个字符串,而 xstream 需要一个字符串数组。

    一个简单的解决方法是将 xml 文件更改为以下行:

    <Crm>
        <phone>
            <string>123456789</string>
        </phone>
        <email>a@a.com</email>
     </Crm>
    

    另一种选择是调整 xstream 并定义一个implicit array,并为String 类定义一个alias

    XStream xstream = new XStream();
    xstream.alias("Crm", CRM.class);
    xstream.addImplicitArray(CRM.class, "phone");
    xstream.alias("phone", String.class);
    

    这将能够将以下 XML 解析为 CRM 实例:

    <Crm>
        <phone>12345</phone>
        <phone>123456</phone>
        <phone>12345678</phone>
        <email>a@a.com</email>
    </Crm>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-13
      • 2013-04-02
      • 2013-03-24
      • 2012-02-25
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多