tags:

views:

214

answers:

0

Hi,

I have a few classes (Class1, Class2, Class3) that I annotated with JAXB annotations (XmlRootElement, XmlElement etc.) to generate XML like this

<class1>
   <field1>ABC</field1>
   <class2>
      <c2Field1>XYZ</c2Field1>
      <class3>
         <c3Field1>1</c3Field1>
      </class3>
      <class3>
         <c3Field1>2</c3Field1>
      </class3>
      ...
   </class2>
</class1>
<class1>
   <field1>DEF</field1>
   <class2>
      <c2Field1>123</c2Field1>
      <class3>
         <c3Field1>3</c3Field1>
      </class3>
      <class3>
         <c3Field1>4</c3Field1>
      </class3>
      ...
   </class2>
</class1>

Serializing these classes to XML works as expected. However, when I want to create these classes from XML, it assigns all the class3 entries to the first class2 entry in the first Class1 instance, even though there are other Class1 instances out there with Class2 instances. In above example, the Class1 instance with the field value (ABC) with Class2 (XYZ) is getting all Class3 instances assigned as children (1,2,3 and 4) and not only (1,2) as expected.

Any ideas why serialization works, but deserialization with the same XML produces different outcome?