Handling different object types contained within the same set of elements

hobheedia

New Member
I have an XML document that looks something like the following:Note that I cannot change the schema because it is part of a standard XML Schema (Library of Congress METS).\[code\]<amdSec ID="AMDSEC001"> <digiprovMD ID="DMD001"> <mdWrap MDTYPE="OBJECT"> <xmlData> <object xsi:type="file"> ..... </object> </xmlData> </mdWrap> </digiprovMD> <digiprovMD ID="DMD001_EVENT"> <mdWrap MDTYPE="EVENT"> <xmlData> <event xsi:type="event"> ..... </event> </xmlData> </mdWrap> </digiprovMD></amdSec>\[/code\]As you can see, the inner element \[code\]<mdWrap>\[/code\] can contain elements of different types; in this case they're \[code\]<event>\[/code\] and \[code\]<object>\[/code\], but it isn't constrained to just those two types. Creating two classes (like below), marshals okay, but this doesn't work for unmarshalling.\[code\]class ObjectMDWrap { @XmlElementWrapper(name = "xmlData") @XmlElement(name = "object") List<MyObject> object; //Wrapped in list to use @XmlElementWrapper}class EventMDWrap { @XmlElementWrapper(name = "xmlData") @XmlElement(name = "event") List<MyEvent> event; //Wrapped in list to use @XmlElementWrapper}\[/code\]What can I do so that JAXB unmarshals the correct "type" of MDWrap?
 
Back
Top