XStream returns 属性值为空

XStream returns attribute value as null

今天我遇到了 XStream 的奇怪行为。所以,XML:

<AddressObjectType KOD_T_ST="0" LEVEL="0" SOCRNAME="blabla"/>

Java class:

@XStreamAlias("AddressObjectType")
public class AddressObjectType {

    @XStreamAsAttribute
    @XStreamAlias("KOD_T_ST")
    private Integer id;

    @XStreamAsAttribute
    @XStreamAlias("SOCRNAME")
    private String name;

    @XStreamAsAttribute
    @XStreamAlias("SCNAME")
    private String shortName;

    @XStreamAsAttribute
    @XStreamAlias("LEVEL")
    private int level;
    // . . . getters, setters
}

我只有属性 KOD_T_ST 有问题。在反序列化期间总是抛出 com.thoughtworks.xstream.converters.ConversionException 并且原因是 NullPointerException。属性值始终为 null。甚至我将类型从 Integer 切换为 String。 到底是怎么回事? XStream 对别名有任何限制吗?如何读取值?

XStream 版本 1.4.11.1.

非常感谢您的帮助和最诚挚的问候。

您现有的 xml 字符串有问题

<AddressObjectType KOD_T_ST="0" LEVEL="0" SOCRNAME="blabla"/>

不使用上面的,而是使用下面的,它会起作用,AddressObjectType 对象模型没有变化。区别一下,我提供的xml中有双下划线(__)。

<AddressObjectType KOD__T__ST="0" LEVEL="0" SOCRNAME="blabla"/>

要理解,请参考下面的link。 http://x-stream.github.io/faq.html#XML_double_underscores

引用,

Why do field names suddenly have double underscores in the generated XML? XStream maps Java class names and field names to XML tags or attributes. Unfortunately this mapping cannot be 1:1, since some characters used for identifiers in Java are invalid in XML names. Therefore XStream uses an XmlFriendlyNameCoder to replace these characters with a replacement. By default this NameCoder uses an underscore as escape character and has therefore to escape the underscore itself also. You may provide a different configured instance of the XmlFriendlyNameCoder or a complete different implementation like the NoNameCoder to prevent name coding at all. However it is your responsibility then to ensure, that the resulting names are valid for XML.