Search code examples
xstreamunmarshalling

Problem unmarshalling an attribute with name "class" with Xstream


I have a node with an attribute named class. The input XML is :

<Job class="com.test.jobImplementation">
  <Priority>1</Priority>
  ......
</Job>

The Java class which represents the XML is annotated with Xstream annotations is as follows:

@XStreamAlias("Job")
public static class Job {

    @XStreamAsAttribute
    @XStreamAlias("class")
    private String implementationClass;

    @XStreamAlias("Priority")
    private Integer priority

}

When I try to deserialize the XML, xstream fails returning an error unrelated to the problem. (e.g. when I replace the attribute name "class" by "classs", it works fine.)

I know the "class" attribute is used whenever XStream can't tell from the XML and the field declaration exactly what type to use, but in this case I can't modify the XML input and I have to process the attribute "class".

Any workaround for unmarshalling an XML attribute with name "class" with Xstream?


Solution

  • Starting with XStream 1.3.1 you can redefine those attributes to allow the names to be used for your own ones.

    http://x-stream.github.io/faq.html#Serialization_system_attributes