I need to deserialize xml which is result of polymorphic classes. Below are 2 examples.
Example 1:
<Root>
<TYPE>TYPE1</TYPE>
<Data>
<data1>fdfdf</data1>
<data2>dfdfdf<data2>
</Data>
</Root>
Example 2:
<Root>
<TYPE>TYPE2</TYPE>
<Data>
<data3>fdfdf</data1>
<data4>dfdfdf<data2>
</Data>
</Root>
My java class structure is
class Root {
String TYPE ;
Data data;
}
class Data {
}
class DataType1 extends Data{
data1;
data2;
}
class DataType2 extends Data{
data3;
data4;
}
Root.TYPE in xml could be used to determine if the data in xml is for for DataType1 or DataType2. XML does not provide any "class" attribute for tag.
I was trying various Strategy classes from simple xml but could not make it work.
Do you have any suggestion how/which strategy or other classes I should use so that I could correctly parse the xml and create proper object's of DataType1 OR DataType2 during deserialization.
Thanks,
Tushar