Manikandan
Manikandan

Reputation: 83

Deserializing using Xstream

Below is my java class

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;
    }

}

and below is my XML.

<Crm>
    <Phone>123456789</Phone>
    <email>[email protected]</email>
</Crm>

The following is the stack trace I am getting:

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

Am I missing something or doing something fundamentally wrong?

I am using xstream-1.4.2.jar and Java 1.6.

I will be always trying to convert xmls to java object.

Upvotes: 1

Views: 2137

Answers (1)

DB5
DB5

Reputation: 13988

Without seeing the code that you use to instantiate XStream I can't be 100% certain, but the problem appears to be because in the XML you are providing a String where xstream is expecting an Array of Strings.

One simple fix is to change the xml file to something along the following lines:

<Crm>
    <phone>
        <string>123456789</string>
    </phone>
    <email>[email protected]</email>
 </Crm>

An alternative option is to tweak xstream and define an implicit array, and also define an alias for the String class:

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

this will then be able to parse the following XML into a CRM instance:

<Crm>
    <phone>12345</phone>
    <phone>123456</phone>
    <phone>12345678</phone>
    <email>[email protected]</email>
</Crm>

Upvotes: 1

Related Questions