Polymorphic XML Binding with JAXB



Here is what I am trying to do:


XML is this:



<Doctype1>
<Outter>
<Inner>
<ProblemType1>somedata</ProblemType1>
</Inner>
</Outter>
</Doctype1>


<-- However, I have: -->



<Doctype2>
<Outter>
<Inner>
<ProblemType2>somedata</ProblemType1>
</Inner>
</Outter>
</Doctype1>


and Share most of the same fields, but in the case of Doctype1 I need ProblemType1 and in Doctype2 I need problem 2.


I want to be able to reuse the classes I bind to for and as they are a common tag across all doc types.



@XmlAccessorType(XmlAccessType.FIELD)
public class Doc1{

@XmlElement(name = "Outter")
public List<Outter> outtards;

}

@XmlAccessorType(XmlAccessType.FIELD)
public class Outter {

@XmlElement(name = "Inner")
public List<Innard> innards;
}

@XmlAccessorType(XmlAccessType.FIELD)
public class Innard{

// need to change this if it's Doc1 or Doc2. The element name won't change
@XmlElement(name = "Inner")
public ProblemType1 subtype;
}


It seems like Maybe a factory is in order?



@XmlType(factoryClass= , factoryMethod=)

No comments:

Post a Comment