问题描述
我有一个具有名为 class
的属性的节点.输入的 XML 是:
I have a node with an attribute named class
. The input XML is :
<Job class="com.test.jobImplementation">
<Priority>1</Priority>
......
</Job>
表示XML的Java类用Xstream注解注解如下:
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
}
当我尝试反序列化 XML 时,xstream 无法返回与问题无关的错误.(例如,当我用类"替换属性名称类"时,它工作正常.)
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.)
我知道每当 XStream 无法从 XML 和字段声明究竟要使用什么类型,但在这种情况下,我无法修改 XML 输入,我必须处理属性class".
I know the "class" attribute is used whenever XStream can't tell from the XML andthe 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".
使用 Xstream 解组名称为class"的 XML 属性的任何解决方法?
Any workaround for unmarshalling an XML attribute with name "class" with Xstream?
推荐答案
从 XStream 1.3.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
这篇关于解组名称为“class"的属性时出现问题使用 Xstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!