我有一个从源XML文件到JAXB对象中的模型的绑定。例如,源XML如下所示:
<school>
...
</school>
经过一些处理后,我的数据模型被嵌套,例如这个
schoolX
courseA
student1
courseB
student1
当我调用binder.updateXml(school)时,即使student1在我的JAXB对象中被分配给Course的两个实例,它在XML中也只会被放置在courseB中。
- 如果学生1被分配到多个课程,这是正常行为吗
- 当我在其中一门课程中使用克隆的student1时,一切都如预期一样
我的绑定如下所示:
<java-type name="School">
<java-attributes>
<xml-element java-attribute="courses" xml-path="course/" type="my.model.Course" container-type="java.util.List"/>
</java-attributes>
</java-type>
<java-type name="Course">
<java-attributes>
<xml-element java-attribute="students" xml-path="student/" type="my.model.Student" container-type="java.util.List"/>
</java-attributes>
</java-type>
<java-type name="Student">
<java-attributes>
<xml-element java-attribute="name" xml-path="@name"/>
</java-attributes>
</java-type>
Wrt Binder
JAXB实现认为对象和节点之间存在1对1的关系。你可以在以下方法中看到这一点:
- http://docs.oracle.com/javase/7/docs/api/javax/xml/bind/Binder.html#getXMLNode(java.lang.Object)
- http://docs.oracle.com/javase/7/docs/api/javax/xml/bind/Binder.html#getJAXBNode(XmlNode)
这就是为什么当您在两个位置有相同的实例时会遇到问题。对于Binder
,JAXB实现期望这两个实例不同。使用Marshaller
时不存在此要求。