JAXB 重复对象
JAXB duplicate object
如何避免在假定对象实例相同时创建不同的对象实例?
这是 xml 文件:
<family>
<person>
<firstName>John</firstName>
<father>
<firstName>Gary</firstName>
</father>
<mother>
<firstName>Jane</firstName>
</mother>
</person>
<person>
<firstName>Philip</firstName>
<father>
<firstName>Franck</firstName>
</father>
<mother>
<firstName>Jane</firstName>
</mother>
</person>
</family>
父元素和母元素的类型为 Person
。
我有两个人和同一个母亲,当我解组 xml 文件时,它创建了两个不同的人实例 class。有没有办法告诉他们应该是同一个对象?
XML 文件可以使用 "pointers" 的等价物。在 XML 架构方面,
<xsd:element name="name" type="xsd:ID"/> <!-- in Person-->
<xsd:element name="persref" type="xsd:IDREF"/> <!-- the "pointer" to a person -->
"pointers"是Person字段的值,必须是唯一的(所以一个人的名字一般是不够的)。不是编组整个对象,而是仅编组 "pointer" 值。完整的对象本身必须单独编组。
在你的情况下,这很简单,因为每个人都作为家庭成员出现一次,而且只出现一次。您只需为父亲和母亲使用 "persref" 元素。 - 有关技术细节,请参阅 this tutorial (also a second section)。
如何避免在假定对象实例相同时创建不同的对象实例?
这是 xml 文件:
<family>
<person>
<firstName>John</firstName>
<father>
<firstName>Gary</firstName>
</father>
<mother>
<firstName>Jane</firstName>
</mother>
</person>
<person>
<firstName>Philip</firstName>
<father>
<firstName>Franck</firstName>
</father>
<mother>
<firstName>Jane</firstName>
</mother>
</person>
</family>
父元素和母元素的类型为 Person
。
我有两个人和同一个母亲,当我解组 xml 文件时,它创建了两个不同的人实例 class。有没有办法告诉他们应该是同一个对象?
XML 文件可以使用 "pointers" 的等价物。在 XML 架构方面,
<xsd:element name="name" type="xsd:ID"/> <!-- in Person-->
<xsd:element name="persref" type="xsd:IDREF"/> <!-- the "pointer" to a person -->
"pointers"是Person字段的值,必须是唯一的(所以一个人的名字一般是不够的)。不是编组整个对象,而是仅编组 "pointer" 值。完整的对象本身必须单独编组。
在你的情况下,这很简单,因为每个人都作为家庭成员出现一次,而且只出现一次。您只需为父亲和母亲使用 "persref" 元素。 - 有关技术细节,请参阅 this tutorial (also a second section)。