Problem unmarshalling an attribute with name “class” with Xstream

自古美人都是妖i 提交于 2019-12-11 04:57:11

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/4923742/problem-unmarshalling-an-attribute-with-name-class-with-xstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!